我正在尝试使用 WinHTTP 通过具有标头和可选的 json 内容的代理发送 http post 请求。我已经尝试了此处问题中概述的解决方案:
WinHttpSendRequest returns ERROR_INVALID_PARAMETER
然而,它似乎并没有改变任何东西。我在下面粘贴了发布请求功能。也许我错过了一些东西。如果我能做些什么来解决这个问题,请告诉我。
问题区域围绕 WinHttpSendRequest
std::string PostRequest(std::wstring token, std::string json, std::string proxy, std::wstring url, std::wstring domain) {
std::cout<<"PostRequest function started"<<std::endl;
std::cout<<"json is being processed"<<std::endl;
LPSTR data = (LPSTR)json.c_str();
DWORD data_len;
if(data!=0) data_len = strlen(data); else data_len = 0;
std::cout<<"json done"<<std::endl<<GetLastError()<<std::endl;
std::string response;
DWORD dwSize = 0;
DWORD dwDownloaded = 0;
LPSTR pszOutBuffer;
BOOL bResults = FALSE;
HINTERNET hConnect = NULL, hRequest = NULL;
std::wstring stemp = std::wstring(proxy.begin(), proxy.end());
const wchar_t* sw = stemp.c_str();
std::cout<<"initializing WinHttpOpen"<<std::endl<<GetLastError()<<std::endl;
HINTERNET hSession=WinHttpOpen(L"WinHTTP Example/1.0", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, L"http://"+*sw, WINHTTP_NO_PROXY_BYPASS, 0);
std::cout<<"initializing WinHttpConnect"<<std::endl<<GetLastError()<<std::endl;
if(hSession) hConnect = WinHttpConnect(hSession, domain.c_str(), INTERNET_DEFAULT_HTTPS_PORT, 0 );
std::cout<<"initializing WinHttpOpenRequest"<<std::endl<<GetLastError()<<std::endl;
if(hConnect) hRequest = WinHttpOpenRequest(hConnect, L"POST", url.c_str(), NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, WINHTTP_FLAG_SECURE);
const wchar_t *additionalHeaders =
L"Content-Type: application/json\r\n"
L"User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11\r\n"
L"Authorization: "+*token.c_str()+*L"\r\n";
DWORD headersLength = -1;
std::cout<<"initializing WinHttpSendRequest"<<std::endl<<GetLastError()<<std::endl;
std::cout<<"bResults: "<<bResults<<std::endl;
LPVOID rqdata;
if(data==0)rqdata = WINHTTP_NO_REQUEST_DATA;
else rqdata=(LPVOID)data;
if (hRequest) bResults = WinHttpSendRequest(hRequest, additionalHeaders, headersLength, rqdata, data_len, data_len, 0);
else std::cout<<"else 1"<<std::endl;
std::cout<<"bResults: "<<bResults<<std::endl;
std::cout<<"initializing WinHttpReceiveResponse"<<std::endl<<GetLastError()<<std::endl;
if (bResults) bResults = WinHttpReceiveResponse(hRequest, NULL);
else std::cout<<"else 2"<<std::endl;
std::cout<<"bResults: "<<bResults<<std::endl;
if (bResults)
{
do
{
// Check for available data.
dwSize = 0;
if (!WinHttpQueryDataAvailable(hRequest, &dwSize))
std::cout<<"Error in WinHttpQueryDataAvailable.\n"<<GetLastError();
// Allocate space for the buffer.
pszOutBuffer = new char[dwSize + 1];
if (!pszOutBuffer)
{
printf("Out of memory\n");
dwSize = 0;
}
else
{
// Read the data.
ZeroMemory(pszOutBuffer, dwSize + 1);
if (!WinHttpReadData(hRequest, (LPVOID)pszOutBuffer,
dwSize, &dwDownloaded))
std::cout<<"Error in WinHttpReadData.\n"<<GetLastError();
else
response = response + std::string(pszOutBuffer);
// Free the memory allocated to the buffer.
delete[] pszOutBuffer;
}
} while (dwSize > 0);
}
if (!bResults)
std::cout<<"Error has occurred.\n"<<GetLastError()<<std::endl;
if (hRequest) WinHttpCloseHandle(hRequest);
if (hConnect) WinHttpCloseHandle(hConnect);
if (hSession) WinHttpCloseHandle(hSession);
return response;
}
所有的 cout 都是为了调试,稍后我会删除。
输出看起来像这样:(对不起,如果有点乱)
PostRequest function started
json is being processed
json done
0
initializing WinHttpOpen
0
initializing WinHttpConnect
0
initializing WinHttpOpenRequest
0
initializing WinHttpSendRequest
0
bResults: 0
bResults: 0
initializing WinHttpReceiveResponse
87
else 2
bResults: 0
Error has occurred.
87