4

I am working on a program that needs to check the existence of a page before it loads (so nothing too exotic).

Everything is working OK, but I cannot get HttpQueryInfo to return a valid status code for a page. The status code returned is: 1875378224

Code producing the problem:

DWORD headerBuffSize = sizeof(DWORD);
DWORD statusCode;
//Check existance of page (for 404 error)
if(!HttpQueryInfo(hRequestHandle,
                  HTTP_QUERY_STATUS_CODE,
                  &statusCode,
                  &headerBuffSize,
                  NULL))
    return 4;

if(statusCode == HTTP_STATUS_NOT_FOUND)
    cout << "We got a 404 error" << endl;

cout << "Got Status code: " << statusCode << endl; //1875378224 everywhere
cout << "404 status code: " << HTTP_STATUS_NOT_FOUND << endl; //What it should be getting

I am not sure what to make of it; I have compared my own code to several examples online, and it looks like it should work, although I may have just made a stupid mistake.

Thanks!

Benoit
  • 76,634
  • 23
  • 210
  • 236
dymk
  • 887
  • 2
  • 10
  • 21
  • What does `sizeof(DWORD)` give you? Just wondering if it's some kind of odd 64/32 bit thing. – sje397 Jul 21 '11 at 14:08
  • 1
    its not a problem with the DWORD itself; it seems to have something to do with the function giving me a 1 byte char array with 0 in it. – dymk Jul 21 '11 at 18:11

3 Answers3

16

As other's have pointed out HttpQueryInfo returns the requested information as a string. You need to ensure that you have a buffer allocated large enough to retrieve the string, and, it would be up to your application to release it.

However, the same Microsoft documentation for HttpQueryInfo also hints that you can get a DWORD for HTTP_QUERY_STATUS_CODE provided HTTP_QUERY_FLAG_NUMBER is used.

The following code snippet shows you how:

DWORD statusCode = 0;
DWORD length = sizeof(DWORD);
HttpQueryInfo(
    hRequestHandle,
    HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER,
    &statusCode,
    &length,
    NULL
);
Stephen Quan
  • 21,481
  • 4
  • 88
  • 75
7

The information retrieved from the response header by HttpQueryInfo is always a text string.

int statusCode;
char responseText[256]; // change to wchar_t for unicode
DWORD responseTextSize = sizeof(responseText);

//Check existance of page (for 404 error)
if(!HttpQueryInfo(hRequestHandle,
                  HTTP_QUERY_STATUS_CODE,
                  &responseText,
                  &responseTextSize,
                  NULL))
    return 4;
statusCode = atoi(responseText);
Captain Obvlious
  • 19,754
  • 5
  • 44
  • 74
  • Thanks for the tip; but now the response text is always zero :/ Also, responseTextSize must be a DWORD, but that shouldn't be what is causing the problem. – dymk Jul 21 '11 at 14:27
  • 1
    If you're compiling for Unicode change `char` to `wchar_t` as well as `atoi()`. I've updated my answer to reflect that along with fixing the issue you pointed out ;) – Captain Obvlious Jul 21 '11 at 14:33
  • Almost got it working; but the char array itself is not holding anything, and the buffer size itself is 1, and contains 0 still. Thanks for your input, very helpful. – dymk Jul 21 '11 at 14:49
  • If you call HttpOpenRequest but forget to also call HttpSendRequest after it, before calling HttpQueryInfo, you will always get a zero value for the status code. – Fernando Echeverria Nov 17 '15 at 15:44
1

I've just recently gotten this to work - found most of the examples on the web didn't work for me, even the ones on MSDN (possibly since my c++ is very rusty at the moment and I was making simple mistakes). This is what I have that works for me:

LPVOID lpOutBuffer = NULL;
DWORD dwSize = 0;

while (!HttpQueryInfo(hRequest, HTTP_QUERY_STATUS_CODE, (LPVOID)lpOutBuffer, &dwSize, NULL))    
{
    DWORD dwError = GetLastError();
    if (dwError == ERROR_INSUFFICIENT_BUFFER)
    {
        lpOutBuffer = new wchar_t[dwSize];  
    }
    else
    {
        fprintf(stderr, "HttpQueryInfo failed, error = %d (0x%x)\n",
            GetLastError(), GetLastError());
        break;
    }
}

wchar_t* outBuffer = (wchar_t*)lpOutBuffer;
std::wcout << L"Status_Code: " << outBuffer;

int status_code = _wtoi(outBuffer);

delete[] lpOutBuffer;
David Hall
  • 32,624
  • 10
  • 90
  • 127