开发者

WinInet HttpQuery Info returning Invalid Status Codes

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

Eve开发者_如何学Crything 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!


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


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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜