C++发送HTTP请求的实现代码
本文导语: 代码如下:#include #include #include #define MAXSIZE 1024#pragma comment(lib, "Wininet.lib") void urlopen(_TCHAR*); int _tmain(int argc, _TCHAR* argv[]){ urlopen(_T("http://coderzh.cnblogs.com")); return 0;} void urlopen(_TCHAR* url){ HINTERNET hSession = InternetOpen(_T("...
#include
#include
#include
#define MAXSIZE 1024
#pragma comment(lib, "Wininet.lib")
void urlopen(_TCHAR*);
int _tmain(int argc, _TCHAR* argv[])
{
urlopen(_T("http://coderzh.cnblogs.com"));
return 0;
}
void urlopen(_TCHAR* url)
{
HINTERNET hSession = InternetOpen(_T("UrlTest"), INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if(hSession != NULL)
{
HINTERNET hHttp = InternetOpenUrl(/tech-cpp/hSession, url, NULL, 0, INTERNET_FLAG_DONT_CACHE, 0/index.html);
if (hHttp != NULL)
{
wprintf_s(_T("%sn"), url);
BYTE Temp[MAXSIZE];
ULONG Number = 1;
while (Number > 0)
{
InternetReadFile(hHttp, Temp, MAXSIZE - 1, &Number);
Temp[Number] = '';
printf("%s", Temp);
}
InternetCloseHandle(hHttp);
hHttp = NULL;
}
InternetCloseHandle(hSession);
hSession = NULL;
}
}
作者:CoderZh