用Winhttp写一个POST https://sellercentral.amazon.com.au/ap/signin 的函数 并且返回响应头和响应内容
时间: 2023-12-07 21:02:15 浏览: 134
好的,以下是使用Winhttp写一个POST请求的函数,并返回响应头和响应内容的代码示例:
```c++
#include <windows.h>
#include <winhttp.h>
#pragma comment(lib, "winhttp.lib")
int main()
{
// 初始化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)
{
printf("WinHttpOpen failed, error %u\n", GetLastError());
return 1;
}
// 创建连接
HINTERNET hConnect = WinHttpConnect(hSession, L"sellercentral.amazon.com.au", INTERNET_DEFAULT_HTTPS_PORT, 0);
if (!hConnect)
{
printf("WinHttpConnect failed, error %u\n", GetLastError());
WinHttpCloseHandle(hSession);
return 1;
}
// 创建请求
HINTERNET hRequest = WinHttpOpenRequest(hConnect, L"POST", L"/ap/signin", NULL, WINHTTP_NO_REFERER, NULL, WINHTTP_FLAG_SECURE);
if (!hRequest)
{
printf("WinHttpOpenRequest failed, error %u\n", GetLastError());
WinHttpCloseHandle(hConnect);
WinHttpCloseHandle(hSession);
return 1;
}
// 设置请求头
LPCWSTR szHeaders = L"Content-Type: application/x-www-form-urlencoded\r\n";
if (!WinHttpAddRequestHeaders(hRequest, szHeaders, (ULONG)-1L, WINHTTP_ADDREQ_FLAG_REPLACE | WINHTTP_ADDREQ_FLAG_ADD))
{
printf("WinHttpAddRequestHeaders failed, error %u\n", GetLastError());
WinHttpCloseHandle(hRequest);
WinHttpCloseHandle(hConnect);
WinHttpCloseHandle(hSession);
return 1;
}
// 设置请求数据
LPCWSTR szData = L"username=myusername&password=mypassword";
if (!WinHttpSendRequest(hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0, (LPVOID)szData, (DWORD)wcslen(szData) * 2, (DWORD)wcslen(szData) * 2, 0))
{
printf("WinHttpSendRequest failed, error %u\n", GetLastError());
WinHttpCloseHandle(hRequest);
WinHttpCloseHandle(hConnect);
WinHttpCloseHandle(hSession);
return 1;
}
// 发送请求
if (!WinHttpReceiveResponse(hRequest, NULL))
{
printf("WinHttpReceiveResponse failed, error %u\n", GetLastError());
WinHttpCloseHandle(hRequest);
WinHttpCloseHandle(hConnect);
WinHttpCloseHandle(hSession);
return 1;
}
// 获取响应头
DWORD dwSize = 0;
if (!WinHttpQueryHeaders(hRequest, WINHTTP_QUERY_RAW_HEADERS_CRLF, WINHTTP_HEADER_NAME_BY_INDEX, NULL, &dwSize, WINHTTP_NO_HEADER_INDEX))
{
if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
{
printf("WinHttpQueryHeaders failed, error %u\n", GetLastError());
WinHttpCloseHandle(hRequest);
WinHttpCloseHandle(hConnect);
WinHttpCloseHandle(hSession);
return 1;
}
}
LPWSTR szHeadersBuffer = new WCHAR[dwSize / 2];
if (!WinHttpQueryHeaders(hRequest, WINHTTP_QUERY_RAW_HEADERS_CRLF, WINHTTP_HEADER_NAME_BY_INDEX, szHeadersBuffer, &dwSize, WINHTTP_NO_HEADER_INDEX))
{
printf("WinHttpQueryHeaders failed, error %u\n", GetLastError());
delete[] szHeadersBuffer;
WinHttpCloseHandle(hRequest);
WinHttpCloseHandle(hConnect);
WinHttpCloseHandle(hSession);
return 1;
}
// 获取响应内容
DWORD dwContentLength = 0;
if (!WinHttpQueryDataAvailable(hRequest, &dwContentLength))
{
printf("WinHttpQueryDataAvailable failed, error %u\n", GetLastError());
delete[] szHeadersBuffer;
WinHttpCloseHandle(hRequest);
WinHttpCloseHandle(hConnect);
WinHttpCloseHandle(hSession);
return 1;
}
LPWSTR szContentBuffer = new WCHAR[dwContentLength / 2 + 1];
ZeroMemory(szContentBuffer, dwContentLength / 2 + 1);
if (!WinHttpReadData(hRequest, szContentBuffer, dwContentLength, NULL))
{
printf("WinHttpReadData failed, error %u\n", GetLastError());
delete[] szHeadersBuffer;
delete[] szContentBuffer;
WinHttpCloseHandle(hRequest);
WinHttpCloseHandle(hConnect);
WinHttpCloseHandle(hSession);
return 1;
}
// 打印响应头和响应内容
wprintf(L"Headers:\n%s\n\n", szHeadersBuffer);
wprintf(L"Content:\n%s\n", szContentBuffer);
// 释放资源
delete[] szHeadersBuffer;
delete[] szContentBuffer;
WinHttpCloseHandle(hRequest);
WinHttpCloseHandle(hConnect);
WinHttpCloseHandle(hSession);
return 0;
}
```
需要注意的是,请求数据需要按照要求进行编码,这里使用的是`application/x-www-form-urlencoded`编码方式,可以根据实际情况进行修改。另外,代码中的用户名和密码需要根据实际情况进行修改。
阅读全文