winhttpreceiverresponse函数返回一个错误

WinHttpReceiveResponse function return an error

本文关键字:一个 错误 函数 返回 winhttpreceiverresponse      更新时间:2023-10-16

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

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,但我正在努力。

当我执行程序时,我得到

无法接收响应

然后,我使用GetLastError()来获得总是为6的错误的代码。我找不到任何关于6号代码的信息吗?

错误码6是ERROR_INVALID_HANDLE "句柄无效。"

问题是您使用错误的变量请求句柄:_hRequest,而您使用hRequest代替。

WinHttpReceiveResponse(_hRequest, ...
相关文章: