Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WinInet HttpQuery Info returning Invalid Status Codes

Tags:

c++

http

wininet

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!

like image 752
dymk Avatar asked Nov 29 '22 10:11

dymk


2 Answers

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
);
like image 133
Stephen Quan Avatar answered Dec 04 '22 10:12

Stephen Quan


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);
like image 31
Captain Obvlious Avatar answered Dec 04 '22 12:12

Captain Obvlious