WinInet HttpQuery Info returning Invalid Status Codes

dymk picture dymk · Jul 21, 2011 · Viewed 9k times · Source

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!

Answer

Stephen Quan picture Stephen Quan · Sep 6, 2012

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
);