VC打开网页,并获取网页内容
2023-04-20
91
0
使用InternetReadFile读取网页内容,注意读到的可能因为网页是UTF-8而需要进行编码转换。
#include <stdio.h>
#include <iostream>
#include <Windows.h>
#include <WinInet.h>
#pragma comment(lib,"Wininet.lib")
int main()
{
HINTERNET hSession;
HINTERNET hRequest;
DWORD dwBytesRead;
BOOL bResult = FALSE;
const char* lpcInterNetURL = "http://www.pnpon.com" ;
char lpwzReadData[10240] = { 0 };
hSession = InternetOpenA(NULL, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if (hSession != NULL)
{
//MessageBoxA(0,"InternetOpenA",0,0);
hRequest = InternetOpenUrlA(hSession, lpcInterNetURL, NULL, 0, INTERNET_FLAG_RELOAD, 0);
if (hRequest != NULL)
{
//MessageBoxA(0,"InternetOpenUrlA",0,0);
if (InternetReadFile(hRequest, lpwzReadData, sizeof(lpwzReadData), &dwBytesRead))
{
//MessageBoxA(0,lpwzReadData,0,0);
bResult = TRUE;
}
InternetCloseHandle(hRequest);
}
InternetCloseHandle(hSession);
}
}