WinInet HttpQuery 信息返回无效状态代码

发布于 2024-11-25 08:29:09 字数 833 浏览 1 评论 0原文

我正在开发一个程序,需要在加载之前检查页面是否存在(所以没有什么太奇特的)。

一切正常,但我无法让 HttpQueryInfo 返回页面的有效状态代码。返回的状态代码是:1875378224

产生问题的代码:

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 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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

终止放荡 2024-12-02 08:29:09

正如其他人指出的 HttpQueryInfo 以字符串形式返回请求的信息。您需要确保分配了足够大的缓冲区来检索字符串,并且由您的应用程序来释放它。

但是,相同的 Microsoft 文档 HttpQueryInfo 还提示您可以获得所提供的 HTTP_QUERY_STATUS_CODEDWORD使用HTTP_QUERY_FLAG_NUMBER

以下代码片段向您展示了如何操作:

DWORD statusCode = 0;
DWORD length = sizeof(DWORD);
HttpQueryInfo(
    hRequestHandle,
    HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER,
    &statusCode,
    &length,
    NULL
);

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
);
在风中等你 2024-12-02 08:29:09

HttpQueryInfo 从响应标头检索的信息始终是文本字符串。

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

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);
℡寂寞咖啡 2024-12-02 08:29:09

我最近才开始工作——发现网络上的大多数示例都不适合我,甚至是 MSDN 上的示例(可能是因为我的 C++ 目前非常生疏,而且我犯了一些简单的错误)。这就是我所拥有的对我有用的东西:

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;

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文