Reprint please declare the source!
Http://www.cnblogs.com/linguanh/category/633252.html
The distance from the last talk of C + + production JSON or other data to the server, two months after the lapse.
Links: http://www.cnblogs.com/linguanh/p/4340119.html
This time it is from the server to get text content to the console, or write local text, such as the operation, nonsense, say, start.
-----------------------------------------------------------Split Line----------------------------------------------------------- --
Test server is: Sina Cloud Sea;
Test content: Get the data that is read from the server by PHP footstep, I am the user's OpenID here;
Tools: VS 2012;
First on the intuitive image, after the source code
General examples
Core functions
For a description of the conversion functions of multibyte wchar to LPCSWTR, go to the link
Http://www.cnblogs.com/linguanh/p/4241939.html
1#include <iostream>2#include <fstream>3#include <Windows.h>4#include <wininet.h>5 #defineMaxblocksize 28+1//OpenID Fixed length6 #pragmaComment (lib, "Wininet.lib")//Introducing Dynamic Libraries7 8 Char* Getweixinfromusernamefromsea (Const Char*);9 using namespacestd;Ten One intMain () { A Char*p=null;//for storing returned results -P=getweixinfromusernamefromsea ("Http://913337456-my.stor.sinaapp.com/xxx.txt"); - thecout<<p; - return 0 ; - } - + //I set the function here with the return value, you can not apply the return value! - Char* Getweixinfromusernamefromsea (Const Char*Url) { + Char*str =New Char[Maxblocksize];//results for the last return, dynamically allocated A Const Char*x="From_af";inti =0;//The first one is the open tag, and I is the following conversion control variable atWCHAR exchange_text_from_url[ the],exchange_text_from_x[ the]; -LPCWSTR py = Exchange_text_from_url;//intermediate variables for URL to LPCWSTR -LPCWSTR PZ = exchange_text_from_x;//Additional Information - //Unicode encoding under Settings, I use a wide byte here, eliminating the hassle of conversion -MultiByteToWideChar (0,0, X,-1, Exchange_text_from_x, -);//WCHAR to LPCWSTR, conversion -MultiByteToWideChar (0,0, URL,-1, Exchange_text_from_url, the ); in //End Conversion -Hinternet handle_for_init_internet = InternetOpen ("From_af", internet_open_type_direct, NULL, NULL,0); to if(Handle_for_init_internet! =NULL) { +Hinternet Handle_for_read_info =InternetOpenUrl (handle_for_init_internet, URL, null,null,null,null); - if(Handle_for_read_info! =NULL) { the CharResult[maxblocksize];//The combination of data used to hold the buffer * CharBuffer[maxblocksize];//buffer for downloading files $DWORD Bytes_read =1;//number of bytes downloadedPanax Notoginseng BOOL Temp_boolean; - while(bytes_read!=0){ the //use InternetReadFile to read data from the buffer to the buffer string, the number of bytes is the effective length of buffer, the control is Bytes_read +Temp_boolean = InternetReadFile (Handle_for_read_info,buffer,sizeof(buffer), &bytes_read); A } the for(i;i<maxblocksize-1; i++){ + if(i==maxblocksize-2&& buffer[i]=='0'){//remove the last interference value 0 - $}Else if(buffer[i]>= the&& buffer[i]<=126){//A variety of tests, the end of the use of ASCII code range judgment to solve the hot stamping ~ ~ ~ $ //cout<<buffer[i];//eliminates the additional promiscuous space of a buffer by using a loop to output a single character specifically - //here is not directly out of buffer because, there are a lot of buffer in the data in the output will become a lot of hot, usually empty will be hot -Result[i]=buffer[i];//after testing, this one-on-each assignment can remove the inclusions in the hot ~ ~ ~ the } - } Wuyiresult[i]=' /';//end of assignment to prevent self-scalding thestrcpy (Str,result);//Copy to a string pointer to return - //safe operation, destroying handle WuInternetCloseHandle (Handle_for_read_info); Handle_for_read_info =NULL; - } AboutInternetCloseHandle (handle_for_init_internet); Handle_for_init_internet =NULL; $ returnstr; - } -}
C + + PHP interaction-----C + + get web page text content, get the echo value of PHP.