gpt4 book ai didi

c++ - WinHttpReceiveResponse 函数返回错误

转载 作者:行者123 更新时间:2023-11-28 06:50:34 25 4
gpt4 key购买 nike

有人能告诉我下面的代码有什么问题吗?

void Mess(LPCWSTR Str)
{
MessageBox(0, Str, L"Hi", 0);
}

int main()
{
// HttpOpen
HINTERNET hHttp = NULL;
LPCWSTR lpcwAgent = L"UserAgent";
DWORD dwAccessType = WINHTTP_ACCESS_TYPE_NO_PROXY;
LPCWSTR lpcwProxyName = WINHTTP_NO_PROXY_NAME;
LPCWSTR lpcwProxyByPass = WINHTTP_NO_PROXY_BYPASS;
DWORD dwFlag = 0;

// HttpConnect
HINTERNET hConnect = NULL;
LPCWSTR lpcwServerName = L"localhost";
INTERNET_PORT Port = INTERNET_DEFAULT_HTTP_PORT;
DWORD dwReserved = 0; //must be 0

// HttpOpenRequest
HINTERNET hRequest = NULL;
LPCWSTR lpcwVerb = L"GET"; // or POST, PUT
LPCWSTR lpcwObjectName = L"/index.php";
LPCWSTR lpcwVersion = NULL; //NULL == HTTP/1.1
LPCWSTR lpcwReferrer = WINHTTP_NO_REFERER; // It mean there's no referrer
LPCWSTR* plpcwAccept = WINHTTP_DEFAULT_ACCEPT_TYPES; // For more information: Media types (http://www.iana.org/assi gnments/medi a-types)
DWORD dwRequestFlag = 0;

// HttpSendRequest
bool bSendRequest = false;
LPCWSTR lpcwHeaders = WINHTTP_NO_ADDITIONAL_HEADERS; // Additional headers to append to the request
DWORD dwHeaderLength = 0;
LPVOID lpvOptional = WINHTTP_NO_REQUEST_DATA; //The data which is used for POST or PUT
DWORD dwOptionalLength = 0;
DWORD dwTotalLength = 0;
DWORD_PTR pdwContext = NULL;

// HttpReceiveResponse
bool bReceiveResponse = false;
HINTERNET _hRequest = NULL; // Will be set again after HttpOpenRequest are completed
LPVOID lpvReserved = NULL;

hHttp = WinHttpOpen(lpcwAgent, dwAccessType, lpcwProxyName, lpcwProxyByPass, dwFlag);
if ( hHttp == NULL )
{
Mess(L"Unable to open a connection");
}
else
{
hConnect = WinHttpConnect(hHttp, lpcwServerName, Port, dwReserved);
if ( hConnect == NULL )
{
Mess(L"Unable to Connecting");
}
else
{
hRequest = WinHttpOpenRequest(hConnect, lpcwVerb, lpcwObjectName, lpcwVersion, lpcwReferrer, plpcwAccept, dwRequestFlag);
if ( hRequest == NULL )
{
Mess(L"Unable to open the request");
}
else
{
bSendRequest = WinHttpSendRequest(hRequest, lpcwHeaders, dwHeaderLength, lpvOptional, dwOptionalLength, dwTotalLength, pdwContext);
if ( !bSendRequest )
{
Mess(L"Failed to send the request");
}
else
{
bReceiveResponse = WinHttpReceiveResponse(_hRequest, lpvReserved);
if ( !bReceiveResponse )
{
Mess(L"Unable to receive the response");
}
else
{
Mess(L"Ok");
}
}
}
}
}

if ( hRequest != NULL )
WinHttpCloseHandle(hRequest);

if ( hConnect != NULL )
WinHttpCloseHandle(hConnect);

if ( hHttp != NULL )
WinHttpCloseHandle(hHttp);

return 0;
}

我不太了解 WINHTTP 但我正在尝试。

当我执行程序时,我得到

Unable to receive the response

然后,我使用 GetLastError() 获取始终为 6 的错误代码。我找不到有关代码 6 的任何信息?

最佳答案

错误代码 6 是 ERROR_INVALID_HANDLE“句柄无效。”

问题是您为请求句柄使用了错误的变量:_hRequest 而您原本应该使用 hRequest

WinHttpReceiveResponse(_hRequest, ...

关于c++ - WinHttpReceiveResponse 函数返回错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24031878/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com