您的位置:首页 > 理论基础 > 计算机网络

VC++获取某个http网页内容

2015-08-04 13:48 417 查看
#include <windows.h>
#include <wininet.h>
#include <fstream>
using namespace std;

#pragma comment(lib,"Wininet.lib")

int main(int argc, char* argv[])
{
TCHAR szUrl[] = _T("http://www.baidu.com");
TCHAR szAgent[] = _T("");
HINTERNET hInternet1 =  InternetOpen(_T("Microsoft Internet Explorer"),INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,NULL);
if (NULL == hInternet1)
{
InternetCloseHandle(hInternet1);
return FALSE;
}
HINTERNET hInternet2 =  InternetOpenUrl(hInternet1,szUrl,NULL,NULL,INTERNET_FLAG_NO_CACHE_WRITE,NULL);
if (NULL == hInternet2)
{
InternetCloseHandle(hInternet2);
InternetCloseHandle(hInternet1);
return FALSE;
}

char szBuffer[1024] = {0};
DWORD dwByteRead = 0;

ofstream ofs(_T("page.html"));

// 循环读取缓冲区内容直到结束
while (InternetReadFile(hInternet2, szBuffer, 1023, &dwByteRead) && dwByteRead > 0)
{
ofs<<szBuffer;
// 清空缓冲区以备下一次读取
ZeroMemory(szBuffer, sizeof(szBuffer));
}
ofs.close();
InternetCloseHandle(hInternet2);
InternetCloseHandle(hInternet1);

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: