2

Response cookies ('Set-Cookie' response header) obtained fine, but request cookies ('Cookie' request header) I can't obtain by WinHttpQueryHeaders even with WINHTTP_QUERY_FLAG_REQUEST_HEADERS:

DWORD size = 0;
BOOL re = WinHttpQueryHeaders(hRequest, WINHTTP_QUERY_COOKIE /*| WINHTTP_QUERY_FLAG_REQUEST_HEADERS*/, nullptr, nullptr, &size, nullptr);
DWORD err = GetLastError(); // re = 0 && err == ERROR_WINHTTP_HEADER_NOT_FOUND

What's wrong?

emett
  • 128
  • 13

2 Answers2

0

This should work:

DWORD lastError;
LPVOID lpBuffer;
DWORD dwSize = 0, dwIndex = 0;

// Call with zero size to get actual size
BOOL res = WinHttpQueryHeaders(hRequest,
        WINHTTP_QUERY_COOKIE,
        WINHTTP_HEADER_NAME_BY_INDEX,
        NULL,
        &dwSize,
        &dwIndex);
lastError = GetLastError();
if (lastError == ERROR_INSUFFICIENT_BUFFER)
{
    lpBuffer = new WCHAR[dwSize/sizeof(WCHAR)];
    WinHttpQueryHeaders(hRequest,
        WINHTTP_QUERY_COOKIE,
        WINHTTP_HEADER_NAME_BY_INDEX,
        lpBuffer,
        &dwSize,
        &dwIndex);

    // convert result to wstring
    std::wstring result(reinterpret_cast<wchar_t*>(lpBuffer),
        dwSize/sizeof(wchar_t));
    std::wcout << L"Result: " << result << endl;
    delete[] lpBuffer;
}
PrashantB
  • 309
  • 2
  • 4
  • 13
0

I was able to retrieve the request headers (including cookies) by using the (WINHTTP_QUERY_FLAG_REQUEST_HEADERS|WINHTTP_QUERY_RAW_HEADERS_CRLF) combination, as opposed just using WINHTTP_QUERY_RAW_HEADERS_CRLF for the response headers.