18,356
社区成员
发帖
与我相关
我的任务
分享
#include <windows.h>
#include "Wininet.h"
#include <iostream>
#include <string>
#pragma comment(lib, "Wininet.lib")
using namespace std;
enum Html_Type
{
CODE_UTF8, //UTF8编码的网站
CODE_GB2312, //GB2312编码的网站
};
string DownHtmlContent(const string &strUrl, Html_Type type);
int main()
{
string strHtml("");
string strUrl = "http://news.sohu.com/20101230/n278604307.shtml";
strHtml = DownHtmlContent(strUrl, CODE_GB2312);
cout << strHtml.c_str() << endl;
}
string DownHtmlContent(const string &strUrl, Html_Type type)
{
string strContent;
string strRooK = "RookIE/1.0";
string strUrlTmp = strUrl;
HINTERNET hSession = InternetOpen("RookIE/1.0", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if (hSession != NULL)
{
HINTERNET handle2 = InternetOpenUrl(hSession, strUrl.c_str(), NULL, 0, INTERNET_FLAG_DONT_CACHE, 0);
if (handle2 != NULL)
{
char *Temp = new char[1024*1024];
ZeroMemory(Temp, 1024*1024);
DWORD Number = 1;
DWORD Total = 0;
BOOL bRes = FALSE;
while ( Number > 0 )
{
if( InternetReadFile(handle2, Temp+Total, 2048, &Number) )
{
Total += Number;
}
}
if ( type == CODE_GB2312 )
{
strContent = Temp;
}
else if ( type == CODE_UTF8 )
{
wchar_t *pUnicode = new wchar_t[1024*1024];
char *pAnsi = new char[1024*1024];
ZeroMemory( pAnsi, 1024*1024 );
ZeroMemory( pUnicode, 1024*1024*2 );
if( MultiByteToWideChar( CP_UTF8, 0, Temp, Total, pUnicode, 1024*1024 ) != 0 )
{
WideCharToMultiByte( CP_ACP, 0, pUnicode, wcslen( pUnicode ), pAnsi, 1024*1024, "", NULL );
strContent = pAnsi;
}
delete []pAnsi;
delete []pUnicode;
}
delete []Temp;
InternetCloseHandle(handle2);
handle2 = NULL;
}
InternetCloseHandle(hSession);
hSession = NULL;
}
return strContent;
}