C++发送HTTP请求的实现代码
#include<stdio.h> #include<windows.h> #include<wininet.h>
#defineMAXSIZE1024 #pragmacomment(lib,"Wininet.lib")
voidurlopen(_TCHAR*);
int_tmain(intargc,_TCHAR*argv[]) { urlopen(_T("http://coderzh.cnblogs.com")); return0; }
voidurlopen(_TCHAR*url) { HINTERNEThSession=InternetOpen(_T("UrlTest"),INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0); if(hSession!=NULL) { HINTERNEThHttp=InternetOpenUrl(hSession,url,NULL,0,INTERNET_FLAG_DONT_CACHE,0);
if(hHttp!=NULL) { wprintf_s(_T("%s\n"),url);
BYTETemp[MAXSIZE]; ULONGNumber=1; while(Number>0) { InternetReadFile(hHttp,Temp,MAXSIZE-1,&Number); Temp[Number]='\0'; printf("%s",Temp); } InternetCloseHandle(hHttp); hHttp=NULL; } InternetCloseHandle(hSession); hSession=NULL; } }