#include "stdafx.h"
#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("IE6.0", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0 );
string url = "http://www.sina.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("wangye.txt");
out_file << revData;
InternetCloseHandle(hHttpFile);
InternetCloseHandle(hINet);
cout << "capture success !/n" << endl;
system("pause");
return 0;
}