用Winhttp写一个POST https://sellercentral.amazon.com.au/ap/signin 的自定义函数 并且返回响应头和响应内容
时间: 2023-11-29 08:03:46 浏览: 34
好的,以下是一个使用 Winhttp 发送 POST 请求到 https://sellercentral.amazon.com.au/ap/signin 的自定义函数,并返回响应头和响应内容的示例代码:
```c++
#include <Windows.h>
#include <winhttp.h>
#include <iostream>
#include <string>
#pragma comment(lib, "winhttp.lib")
std::string PerformPostRequest()
{
// 创建 Winhttp 会话
HINTERNET hSession = WinHttpOpen(L"WinHTTP Example/1.0", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);
if (!hSession)
{
std::cerr << "Failed to open WinHTTP session" << std::endl;
return "";
}
// 创建 Winhttp 连接
HINTERNET hConnect = WinHttpConnect(hSession, L"sellercentral.amazon.com.au", INTERNET_DEFAULT_HTTPS_PORT, 0);
if (!hConnect)
{
std::cerr << "Failed to connect to server" << std::endl;
WinHttpCloseHandle(hSession);
return "";
}
// 创建 Winhttp 请求
HINTERNET hRequest = WinHttpOpenRequest(hConnect, L"POST", L"/ap/signin", NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, WINHTTP_FLAG_SECURE);
if (!hRequest)
{
std::cerr << "Failed to create HTTP request" << std::endl;
WinHttpCloseHandle(hConnect);
WinHttpCloseHandle(hSession);
return "";
}
// 设置请求头
LPCWSTR lpszHeaders = L"Content-Type: application/x-www-form-urlencoded\r\n";
// 设置请求数据
std::string strData = "username=myusername&password=mypassword";
// 发送请求
BOOL bResults = WinHttpSendRequest(hRequest, lpszHeaders, -1L, (LPVOID)strData.c_str(), strData.length(), strData.length(), 0);
if (!bResults)
{
std::cerr << "Failed to send HTTP request" << std::endl;
WinHttpCloseHandle(hRequest);
WinHttpCloseHandle(hConnect);
WinHttpCloseHandle(hSession);
return "";
}
// 等待响应
bResults = WinHttpReceiveResponse(hRequest, NULL);
if (!bResults)
{
std::cerr << "Failed to receive HTTP response" << std::endl;
WinHttpCloseHandle(hRequest);
WinHttpCloseHandle(hConnect);
WinHttpCloseHandle(hSession);
return "";
}
// 获取响应头
DWORD 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 lpOutBuffer = new WCHAR[dwSize / sizeof(WCHAR)];
if (!WinHttpQueryHeaders(hRequest, WINHTTP_QUERY_RAW_HEADERS_CRLF, WINHTTP_HEADER_NAME_BY_INDEX, lpOutBuffer, &dwSize, WINHTTP_NO_HEADER_INDEX))
{
std::cerr << "Failed to retrieve HTTP headers" << std::endl;
delete[] lpOutBuffer;
WinHttpCloseHandle(hRequest);
WinHttpCloseHandle(hConnect);
WinHttpCloseHandle(hSession);
return "";
}
std::wcout << L"Response Headers:" << std::endl << lpOutBuffer << std::endl;
delete[] lpOutBuffer;
}
// 获取响应内容
std::string strResponse;
DWORD dwBytesRead = 0;
do
{
char szBuffer[1024] = { 0 };
bResults = WinHttpReadData(hRequest, szBuffer, sizeof(szBuffer) - 1, &dwBytesRead);
if (bResults)
{
szBuffer[dwBytesRead] = '\0';
strResponse += szBuffer;
}
else
{
std::cerr << "Failed to read HTTP response" << std::endl;
WinHttpCloseHandle(hRequest);
WinHttpCloseHandle(hConnect);
WinHttpCloseHandle(hSession);
return "";
}
} while (dwBytesRead > 0);
// 关闭 Winhttp 句柄
WinHttpCloseHandle(hRequest);
WinHttpCloseHandle(hConnect);
WinHttpCloseHandle(hSession);
return strResponse;
}
int main()
{
std::string strResponse = PerformPostRequest();
std::cout << "Response Content:" << std::endl << strResponse << std::endl;
return 0;
}
```
该函数使用 Winhttp 库中的函数进行 HTTPS 请求,发送 POST 请求的数据为 "username=myusername&password=mypassword",请求头中设置了 "Content-Type: application/x-www-form-urlencoded",并在函数中获取了响应头和响应内容,最终返回响应内容。