64,281
社区成员
发帖
与我相关
我的任务
分享
#include<windows.h>
#include<Wininet.h>
#include<iostream>
#include<fstream>
#include<string>
#pragma comment(lib,"WinInet.lib")
using namespace std;
int main()
{
HINTERNET hINet, hHttpFile;
char szSizeBuffer[32];
DWORD dwLengthSizeBuffer = sizeof(szSizeBuffer);
hINet = InternetOpen("InetURL/1.0", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0 );
string url = "http://www.sohu.com";
if ( !hINet )
{
cout << "InternetOpen fail" << endl;
return 1;
}
hHttpFile = InternetOpenUrl(hINet, url.c_str(), NULL, 0, 0, 0);
if(!hHttpFile)
{
cout << "error open url" << endl;
return 1;
}
BOOL bQuery = HttpQueryInfo(hHttpFile,
HTTP_QUERY_CONTENT_LENGTH,
szSizeBuffer,
&dwLengthSizeBuffer, NULL);
if(bQuery ==false)
{
InternetCloseHandle(hINet);
cout << "error query info" << endl;
return 3;
}
int FileSize=atol(szSizeBuffer);
string revData;
revData.resize(FileSize);
DWORD dwBytesRead;
BOOL bRead = InternetReadFile(hHttpFile, &revData[0], FileSize, &dwBytesRead);
if(!bRead)
{
cout << "error to read file" << endl;
return 4;
}
ofstream out_file("sohu.html");
out_file << revData;
InternetCloseHandle(hHttpFile);
InternetCloseHandle(hINet);
cout << "success" << endl;
system("pause");
return 0;
}