使用WinHTTP(C ++)获取请求标头

时间:2018-02-16 09:31:41

标签: c winhttp

我有一个场景,我需要查看发送到服务器的请求标头。

WinhttpQueryHeaders 中使用选项标志 WINHTTP_QUERY_FLAG_REQUEST_HEADERS ,我尝试了但是收到错误 12150 (ERROR_WINHTTP_HEADER_NOT_FOUND)。

我的代码(假设hRequest的值不为空):

LPVOID getRequestHeaders(HINTERNET hRequest)
{
    LPVOID tempHeaderBuffer = NULL;
    DWORD dwSize = 0;

    WinHttpQueryHeaders(hRequest, WINHTTP_QUERY_URI | WINHTTP_QUERY_FLAG_REQUEST_HEADERS, WINHTTP_HEADER_NAME_BY_INDEX, NULL, &dwSize, WINHTTP_NO_HEADER_INDEX);

    cout << GetLastError();

    if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
    {
        tempHeaderBuffer = (LPWSTR)malloc(dwSize * sizeof(LPWSTR) + 5);

        if (!WinHttpQueryHeaders(hRequest, WINHTTP_QUERY_URI | WINHTTP_QUERY_FLAG_REQUEST_HEADERS, WINHTTP_HEADER_NAME_BY_INDEX, tempHeaderBuffer, &dwSize, WINHTTP_NO_HEADER_INDEX))
        {
            cout << GetLastError();
        }
        else
        {
            printf("GetHTTPResponse : Header Request Contents : \n\n%S", tempHeaderBuffer);
        }
    }

    return tempHeaderBuffer;
}

我在这里做错了什么?有什么建议?我提到了其他类似的问题,但它们都没有解决。

1 个答案:

答案 0 :(得分:2)

我尝试使用WINHTTP_QUERY_URI,但标题似乎不存在。

以下有效:

void main() {
    DWORD dwSize = 0;
    LPVOID lpOutBuffer = NULL;
    BOOL  bResults = FALSE;
    HINTERNET hSession = NULL,
        hConnect = NULL,
        hRequest = NULL;

    // Use WinHttpOpen to obtain a session handle.
    hSession = WinHttpOpen(L"A WinHTTP Example Program/1.0",
        WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
        WINHTTP_NO_PROXY_NAME,
        WINHTTP_NO_PROXY_BYPASS, 0);

    // Specify an HTTP server.
    if (hSession)
        hConnect = WinHttpConnect(hSession, L"localhost",
            INTERNET_DEFAULT_HTTP_PORT, 0);

    // Create an HTTP request handle.
    if (hConnect)
        hRequest = WinHttpOpenRequest(hConnect, L"GET", NULL,
            NULL, WINHTTP_NO_REFERER,
            WINHTTP_DEFAULT_ACCEPT_TYPES,
            0);

    // Send a request.
    if (hRequest)
        bResults = WinHttpSendRequest(hRequest,
            WINHTTP_NO_ADDITIONAL_HEADERS,
            0, WINHTTP_NO_REQUEST_DATA, 0,
            0, 0);

    // End the request.
    if (bResults)
        bResults = WinHttpReceiveResponse(hRequest, NULL);

    // First, use WinHttpQueryHeaders to obtain the size of the buffer.
    if (bResults)
    {
        WinHttpQueryHeaders(hRequest, WINHTTP_QUERY_RAW_HEADERS_CRLF |  WINHTTP_QUERY_FLAG_REQUEST_HEADERS,
            WINHTTP_HEADER_NAME_BY_INDEX, NULL,
            &dwSize, WINHTTP_NO_HEADER_INDEX);

        // Allocate memory for the buffer.
        if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
        {
            lpOutBuffer = new WCHAR[dwSize / sizeof(WCHAR)];

            // Now, use WinHttpQueryHeaders to retrieve the header.
            bResults = WinHttpQueryHeaders(hRequest,  WINHTTP_QUERY_RAW_HEADERS_CRLF |
                WINHTTP_QUERY_FLAG_REQUEST_HEADERS,
                WINHTTP_HEADER_NAME_BY_INDEX,
                lpOutBuffer, &dwSize,
                WINHTTP_NO_HEADER_INDEX);
        }
    }

    // Print the header contents.
    if (bResults)
        printf("Header contents: \n%S", lpOutBuffer);

    // Free the allocated memory.
    delete[] lpOutBuffer;

    // Report any errors.
    if (!bResults)
        printf("Error %d has occurred.\n", GetLastError());

    // Close any open handles.
    if (hRequest) WinHttpCloseHandle(hRequest);
    if (hConnect) WinHttpCloseHandle(hConnect);
    if (hSession) WinHttpCloseHandle(hSession);
}

结果:

Header contents:
GET /dashboard/ HTTP/1.1
User-Agent: A WinHTTP Example Program/1.0
Connection: Keep-Alive