用WinHttp 写一个POST和GET访问https://sellercentral.amazon.com.au/ap/signin 自定义函数。参数有访问方式,提交数据和访问头,并且返回响应内容和响应头,代码使用 Unicode 字符集
时间: 2024-01-14 13:04:59 浏览: 64
GET 方式提交的含有特殊字符的参数
5星 · 资源好评率100%
以下是使用WinHttp库编写的Unicode字符集的POST和GET请求函数的示例代码:
```
#include <windows.h>
#include <winhttp.h>
#pragma comment(lib, "winhttp.lib")
BOOL SendRequest(LPCWSTR lpwszUrl, LPCWSTR lpwszMethod, LPCWSTR lpwszData, LPCWSTR lpwszHeader, LPWSTR lpwszResponseHeader, DWORD dwHeaderLen, LPWSTR lpwszResponseData, DWORD dwDataLen)
{
HINTERNET hSession = NULL, hConnect = NULL, hRequest = NULL;
BOOL bResult = FALSE;
// 打开一个HTTP会话
hSession = WinHttpOpen(L"user-agent", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);
if (!hSession)
{
goto Exit;
}
// 建立到目标服务器的连接
hConnect = WinHttpConnect(hSession, lpwszUrl, INTERNET_DEFAULT_HTTPS_PORT, 0);
if (!hConnect)
{
goto Exit;
}
// 创建一个请求句柄
hRequest = WinHttpOpenRequest(hConnect, lpwszMethod, lpwszUrl, NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, WINHTTP_FLAG_SECURE);
if (!hRequest)
{
goto Exit;
}
// 添加请求头
if (lpwszHeader)
{
if (!WinHttpAddRequestHeaders(hRequest, lpwszHeader, wcslen(lpwszHeader), WINHTTP_ADDREQ_FLAG_REPLACE))
{
goto Exit;
}
}
// 发送请求
if (!WinHttpSendRequest(hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0, (LPVOID)lpwszData, wcslen(lpwszData), wcslen(lpwszData) * sizeof(WCHAR), 0))
{
goto Exit;
}
// 接收响应头
if (lpwszResponseHeader && dwHeaderLen > 0)
{
if (!WinHttpReceiveResponse(hRequest, NULL))
{
goto Exit;
}
if (!WinHttpQueryHeaders(hRequest, WINHTTP_QUERY_RAW_HEADERS_CRLF, WINHTTP_HEADER_NAME_BY_INDEX, lpwszResponseHeader, &dwHeaderLen, WINHTTP_NO_HEADER_INDEX))
{
goto Exit;
}
}
// 接收响应数据
if (lpwszResponseData && dwDataLen > 0)
{
DWORD dwTotalSize = 0;
DWORD dwBytesRead = 0;
LPBYTE lpBuffer = (LPBYTE)lpwszResponseData;
while (WinHttpQueryDataAvailable(hRequest, &dwTotalSize) && dwTotalSize > 0)
{
if (dwTotalSize > dwDataLen - dwBytesRead)
{
goto Exit;
}
if (!WinHttpReadData(hRequest, lpBuffer + dwBytesRead, dwTotalSize, &dwBytesRead))
{
goto Exit;
}
dwBytesRead += dwTotalSize;
}
lpwszResponseData[dwBytesRead / sizeof(WCHAR)] = L'\0';
}
bResult = TRUE;
Exit:
if (hRequest)
{
WinHttpCloseHandle(hRequest);
}
if (hConnect)
{
WinHttpCloseHandle(hConnect);
}
if (hSession)
{
WinHttpCloseHandle(hSession);
}
return bResult;
}
BOOL HttpGet(LPCWSTR lpwszUrl, LPCWSTR lpwszHeader, LPWSTR lpwszResponseHeader, DWORD dwHeaderLen, LPWSTR lpwszResponseData, DWORD dwDataLen)
{
return SendRequest(lpwszUrl, L"GET", NULL, lpwszHeader, lpwszResponseHeader, dwHeaderLen, lpwszResponseData, dwDataLen);
}
BOOL HttpPost(LPCWSTR lpwszUrl, LPCWSTR lpwszData, LPCWSTR lpwszHeader, LPWSTR lpwszResponseHeader, DWORD dwHeaderLen, LPWSTR lpwszResponseData, DWORD dwDataLen)
{
return SendRequest(lpwszUrl, L"POST", lpwszData, lpwszHeader, lpwszResponseHeader, dwHeaderLen, lpwszResponseData, dwDataLen);
}
```
示例用法:
```
int main()
{
// GET请求
wchar_t wszResponseHeader[1024] = { 0 };
wchar_t wszResponseData[1024] = { 0 };
HttpGet(L"https://sellercentral.amazon.com.au/ap/signin", NULL, wszResponseHeader, _countof(wszResponseHeader), wszResponseData, _countof(wszResponseData));
wprintf(L"%s\n%s\n", wszResponseHeader, wszResponseData);
// POST请求
wchar_t wszData[] = L"param1=value1¶m2=value2";
HttpPost(L"https://sellercentral.amazon.com.au/ap/signin", wszData, L"Content-Type: application/x-www-form-urlencoded", wszResponseHeader, _countof(wszResponseHeader), wszResponseData, _countof(wszResponseData));
wprintf(L"%s\n%s\n", wszResponseHeader, wszResponseData);
return 0;
}
```
阅读全文