[C++] 纯文本查看 复制代码
const IID IID_IWinHttpRequest =
{
0x06f29373,
0x5c5a,
0x4b54,
{0xb0, 0x25, 0x6e, 0xf1, 0xbf, 0x8a, 0xbf, 0x0e}
};
int main() {
// Variable for return value
HRESULT hr;
// Initialize COM.
hr = CoInitialize(NULL);
IWinHttpRequest * pIWinHttpRequest = NULL;
BSTR bstrResponse = NULL;
VARIANT varFalse;
VARIANT varEmpty;
CLSID clsid;
VariantInit(&varFalse);
V_VT(&varFalse) = VT_BOOL;
V_BOOL(&varFalse) = VARIANT_FALSE;
VariantInit(&varEmpty);
V_VT(&varEmpty) = VT_ERROR;
hr = CLSIDFromProgID(L"WinHttp.WinHttpRequest.5.1", &clsid);
if (SUCCEEDED(hr)) {
hr = CoCreateInstance(clsid, NULL,
CLSCTX_INPROC_SERVER,
IID_IWinHttpRequest,
(void **)&pIWinHttpRequest);
}
if (SUCCEEDED(hr)) { // Open WinHttpRequest.
//设置超时
pIWinHttpRequest->SetTimeouts(15000, 15000, 15000, 15000);
//访问方式
BSTR bstrMethod = SysAllocString(L"GET");
//网址
BSTR bstrUrl = SysAllocString(L"https://www.jiandaoyun.com/signin");
hr = pIWinHttpRequest->Open(bstrMethod, bstrUrl, varFalse);
//禁止重定向
VARIANT varRedirects;
VariantInit(&varRedirects);
V_VT(&varFalse) = VT_BOOL;
V_BOOL(&varRedirects) = VARIANT_FALSE;
pIWinHttpRequest->put_Option(WinHttpRequestOption::WinHttpRequestOption_EnableRedirects, varRedirects);
//忽略错误标志
VARIANT varSslErrorFlag;
varSslErrorFlag.vt = VT_INT;
varSslErrorFlag.intVal = SslErrorFlag_Ignore_All;
pIWinHttpRequest->put_Option(WinHttpRequestOption::WinHttpRequestOption_SslErrorIgnoreFlags, varSslErrorFlag);
//提交Cookie
pIWinHttpRequest->SetRequestHeader(SysAllocString(L"Cookie"), SysAllocString(L""));
//提交协议头
pIWinHttpRequest->SetRequestHeader(SysAllocString(L"Host"), SysAllocString(L"www.jiandaoyun.com"));
pIWinHttpRequest->SetRequestHeader(SysAllocString(L"Sec-Ch-Ua-Mobile"), SysAllocString(L"?0"));
pIWinHttpRequest->SetRequestHeader(SysAllocString(L"Upgrade-Insecure-Requests"), SysAllocString(L"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36"));
pIWinHttpRequest->SetRequestHeader(SysAllocString(L"User-Agent"), SysAllocString(L"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"));
pIWinHttpRequest->SetRequestHeader(SysAllocString(L"Accept"), SysAllocString(L"none"));
pIWinHttpRequest->SetRequestHeader(SysAllocString(L"Sec-Fetch-Site"), SysAllocString(L"?0"));
pIWinHttpRequest->SetRequestHeader(SysAllocString(L"Sec-Fetch-Mode"), SysAllocString(L"navigate"));
pIWinHttpRequest->SetRequestHeader(SysAllocString(L"Sec-Fetch-User"), SysAllocString(L"?1"));
pIWinHttpRequest->SetRequestHeader(SysAllocString(L"Sec-Fetch-Dest"), SysAllocString(L"document"));
pIWinHttpRequest->SetRequestHeader(SysAllocString(L"Accept-Language"), SysAllocString(L"zh-CN,zh;q=0.9"));
pIWinHttpRequest->SetRequestHeader(SysAllocString(L"Referer"), SysAllocString(L"https://www.jiandaoyun.com/signin"));
//使用SysFreeString连续释放一个BSTR
SysFreeString(bstrMethod);
SysFreeString(bstrUrl);
}
if (SUCCEEDED(hr)) { // 发送请求。
hr = pIWinHttpRequest->Send(varEmpty);
}
if (SUCCEEDED(hr)) { // 获取响应文本。
hr = pIWinHttpRequest->get_ResponseText(&bstrResponse);
}
if (SUCCEEDED(hr)) { // 获取响应文本。
//返回Cookies
BSTR bstrResponse1 = NULL;
pIWinHttpRequest->GetAllResponseHeaders(&bstrResponse1);
}
if (SUCCEEDED(hr)) { // 获取响应文本。
//网页返回的状态代码,例如:200;302;404等
long stat;
pIWinHttpRequest->get_Status(&stat);
}
std::wstring ws(bstrResponse, SysStringLen(bstrResponse));
// 将响应打印到控制台。
std::wcout << ws << std::endl;
// 释放内存。
if (pIWinHttpRequest)
pIWinHttpRequest->Release();
if (bstrResponse)
SysFreeString(bstrResponse);
CoUninitialize();