#include <iostream>
#include <windows.h>
#include <wininet.h>
using namespace std;
string HttpRequest(string strUrl, string strMethod="GET", string strPostData="", int nPostDataLen=0, short sPort=80)
{
int pos;
BOOL bRet;
char *lpHostName, *lpUrl, *lpMethod, *lpPostData;
string tmpUrl, strResponse = "";
while ((pos = strUrl.find(" ")) != strUrl.npos) strUrl.erase(pos, 1); //删除网址中的空格
if (strUrl.substr(0,7) == "http://") strUrl=strUrl.substr(7, strUrl.size()-1); //删除网址中的协议名
if (strUrl.substr(0,8) == "https://") strUrl=strUrl.substr(8, strUrl.size()-1);
if (strUrl.empty()) return strResponse;
if ((pos=strUrl.find("/")) != strUrl.npos){
tmpUrl = strUrl.substr(pos+1, strUrl.size()-1);
strUrl = strUrl.substr(0, pos);
} else tmpUrl = "/";
lpUrl = (char*)tmpUrl.data();
lpMethod = (char*)strMethod.data();
lpHostName = (char*)strUrl.data();
lpPostData = (char*)strPostData.data();
HINTERNET hInternet, hConnect, hRequest;
hInternet = hConnect = hRequest = NULL;
hInternet = (HINSTANCE)InternetOpen("User-Agent", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if (!hInternet) return strResponse;
hConnect = (HINSTANCE)InternetConnect(hInternet, lpHostName, sPort, NULL, "HTTP/1.1", INTERNET_SERVICE_HTTP, 0, 0);
if (!hConnect){
if (hInternet) InternetCloseHandle(hInternet);
return strResponse;
}
hRequest = (HINSTANCE)HttpOpenRequest(hConnect, lpMethod, lpUrl, "HTTP/1.1", NULL, NULL, INTERNET_FLAG_RELOAD, 0);
if (!hRequest){
if (hInternet) InternetCloseHandle(hInternet);
if (hConnect) InternetCloseHandle(hConnect);
return strResponse;
}
bRet = HttpSendRequest(hRequest, NULL, 0, lpPostData, nPostDataLen);
while (true){
char cReadBuffer[4096];
unsigned long lNumberOfBytesRead;
bRet = InternetReadFile(hRequest, cReadBuffer, sizeof(cReadBuffer)-1, &lNumberOfBytesRead);
if (!bRet || !lNumberOfBytesRead) break;
cReadBuffer[lNumberOfBytesRead] = 0;
strResponse = strResponse + cReadBuffer;
}
if (hRequest) InternetCloseHandle(hRequest);
if (hConnect) InternetCloseHandle(hConnect);
if (hInternet) InternetCloseHandle(hInternet);
return strResponse;
}
string UTF8toGB(string s)
{
WCHAR *strSrc;
LPSTR szRes;
char *str = (char*)s.data();
int i = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0);
strSrc = new WCHAR[i+1];
MultiByteToWideChar(CP_UTF8, 0, str, -1, strSrc, i);
i = WideCharToMultiByte(CP_ACP, 0, strSrc, -1, NULL, 0, NULL, NULL);
szRes = new CHAR[i+1];
WideCharToMultiByte(CP_ACP, 0, strSrc, -1, szRes, i, NULL, NULL);
s = szRes;
delete[]strSrc;
delete[]szRes;
return s;
}
int main(void)
{
string utf = "<meta charset=\"utf-8\">";
string url = "https://blog.csdn.net/boysoft2002/article/details/113839813";
string Html = HttpRequest(url);
if (Html.find(utf)!=Html.npos)
cout << UTF8toGB(Html);
else
cout << Html;
return 0;
}
本代码在Dev-C++ 5.11上通过无错编译,请在工具菜单-"编译器选项"里添加如下命令:-std=c++11 -lwininet
如使用vs系列编译器请using namespace std;之前插入:#pragma comment(lib,"WinInet.lib")