WinInet HttpQuery 信息返回无效状态代码
我正在开发一个程序,需要在加载之前检查页面是否存在(所以没有什么太奇特的)。
一切正常,但我无法让 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如其他人指出的 HttpQueryInfo 以字符串形式返回请求的信息。您需要确保分配了足够大的缓冲区来检索字符串,并且由您的应用程序来释放它。
但是,相同的 Microsoft 文档 HttpQueryInfo 还提示您可以获得所提供的
HTTP_QUERY_STATUS_CODE
的DWORD
使用HTTP_QUERY_FLAG_NUMBER
。以下代码片段向您展示了如何操作:
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
forHTTP_QUERY_STATUS_CODE
providedHTTP_QUERY_FLAG_NUMBER
is used.The following code snippet shows you how:
HttpQueryInfo
从响应标头检索的信息始终是文本字符串。The information retrieved from the response header by
HttpQueryInfo
is always a text string.我最近才开始工作——发现网络上的大多数示例都不适合我,甚至是 MSDN 上的示例(可能是因为我的 C++ 目前非常生疏,而且我犯了一些简单的错误)。这就是我所拥有的对我有用的东西:
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: