写个WinHttp 访问https://www.amazon.com.au/ap/signin 返回响应内容和Headers
时间: 2023-07-06 17:21:49 浏览: 173
WinHttp是一个微软开发的用于HTTP客户端和服务器应用程序的API,可以在Windows平台上使用。下面是一个使用WinHttp进行HTTPS请求的示例代码,请求https://www.amazon.com.au/ap/signin页面,并返回响应内容和Headers。
```c++
#include <windows.h>
#include <winhttp.h>
#include <iostream>
#pragma comment(lib, "winhttp.lib")
using namespace std;
int main()
{
HINTERNET hSession, hConnect, hRequest;
DWORD dwSize, dwDownloaded;
LPSTR pszOutBuffer;
// 创建一个WinHTTP会话句柄
hSession = WinHttpOpen(L"WinHTTP Example/1.0", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);
if (!hSession)
{
cout << "WinHttpOpen failed: " << GetLastError() << endl;
return 1;
}
// 创建一个HTTPS连接句柄
hConnect = WinHttpConnect(hSession, L"www.amazon.com.au", INTERNET_DEFAULT_HTTPS_PORT, 0);
if (!hConnect)
{
cout << "WinHttpConnect failed: " << GetLastError() << endl;
WinHttpCloseHandle(hSession);
return 1;
}
// 创建一个HTTPS请求句柄
hRequest = WinHttpOpenRequest(hConnect, L"GET", L"/ap/signin", NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, WINHTTP_FLAG_SECURE);
if (!hRequest)
{
cout << "WinHttpOpenRequest failed: " << GetLastError() << endl;
WinHttpCloseHandle(hConnect);
WinHttpCloseHandle(hSession);
return 1;
}
// 发送HTTPS请求
if (!WinHttpSendRequest(hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0, WINHTTP_NO_REQUEST_DATA, 0, 0, 0))
{
cout << "WinHttpSendRequest failed: " << GetLastError() << endl;
WinHttpCloseHandle(hRequest);
WinHttpCloseHandle(hConnect);
WinHttpCloseHandle(hSession);
return 1;
}
// 等待服务器响应
if (!WinHttpReceiveResponse(hRequest, NULL))
{
cout << "WinHttpReceiveResponse failed: " << GetLastError() << endl;
WinHttpCloseHandle(hRequest);
WinHttpCloseHandle(hConnect);
WinHttpCloseHandle(hSession);
return 1;
}
// 查询响应头
dwSize = 0;
WinHttpQueryHeaders(hRequest, WINHTTP_QUERY_RAW_HEADERS_CRLF, WINHTTP_HEADER_NAME_BY_INDEX, NULL, &dwSize, WINHTTP_NO_HEADER_INDEX);
if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
{
LPWSTR pszHeaders = new WCHAR[dwSize / sizeof(WCHAR)];
if (WinHttpQueryHeaders(hRequest, WINHTTP_QUERY_RAW_HEADERS_CRLF, WINHTTP_HEADER_NAME_BY_INDEX, pszHeaders, &dwSize, WINHTTP_NO_HEADER_INDEX))
{
wcout << L"Headers: " << endl << pszHeaders << endl;
}
else
{
cout << "WinHttpQueryHeaders failed: " << GetLastError() << endl;
}
delete[] pszHeaders;
}
// 查询响应内容
dwSize = 0;
do
{
if (!WinHttpQueryDataAvailable(hRequest, &dwSize))
{
cout << "WinHttpQueryDataAvailable failed: " << GetLastError() << endl;
WinHttpCloseHandle(hRequest);
WinHttpCloseHandle(hConnect);
WinHttpCloseHandle(hSession);
return 1;
}
// 分配缓冲区
pszOutBuffer = new char[dwSize + 1];
if (!pszOutBuffer)
{
cout << "Out of memory" << endl;
WinHttpCloseHandle(hRequest);
WinHttpCloseHandle(hConnect);
WinHttpCloseHandle(hSession);
return 1;
}
// 读取响应内容
ZeroMemory(pszOutBuffer, dwSize + 1);
if (!WinHttpReadData(hRequest, (LPVOID)pszOutBuffer, dwSize, &dwDownloaded))
{
cout << "WinHttpReadData failed: " << GetLastError() << endl;
delete[] pszOutBuffer;
WinHttpCloseHandle(hRequest);
WinHttpCloseHandle(hConnect);
WinHttpCloseHandle(hSession);
return 1;
}
// 输出响应内容
cout << pszOutBuffer;
// 释放缓冲区
delete[] pszOutBuffer;
} while (dwSize > 0);
// 关闭请求句柄
WinHttpCloseHandle(hRequest);
// 关闭连接句柄
WinHttpCloseHandle(hConnect);
// 关闭会话句柄
WinHttpCloseHandle(hSession);
return 0;
}
```
在上面的代码中,我们首先创建了一个WinHTTP会话句柄,然后创建了一个HTTPS连接句柄,接着创建了一个HTTPS请求句柄。我们使用WinHttpSendRequest函数发送了一个GET请求,然后使用WinHttpReceiveResponse函数等待服务器响应。在响应头和响应内容中,我们使用WinHttpQueryHeaders和WinHttpReadData函数分别进行查询和读取。最后,我们关闭了请求句柄、连接句柄和会话句柄。
阅读全文