我正在研究一个程序,它需要在加载之前检查页面是否存在(所以没有太多异国情调)。
一切正常,但我无法让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
我不知道该怎么做;我已将自己的代码与在线的几个示例进行比较,看起来应该工作,尽管我可能犯了一个愚蠢的错误。
谢谢!
答案 0 :(得分:10)
HttpQueryInfo的Microsoft文档提示您可以获得DWORD
HTTP_QUERY_STATUS_CODE
提供的HTTP_QUERY_FLAG_NUMBER
DWORD statusCode = 0;
DWORD length = sizeof(DWORD);
HttpQueryInfo(
hRequestHandle,
HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER,
&statusCode,
&length,
NULL
);
。以下代码段显示了如何:
{{1}}
答案 1 :(得分:6)
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);
答案 2 :(得分:1)
我最近才开始工作 - 发现网络上的大多数示例都不适用于我,即使是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;