使用带有WinHttpQueryHeaders函数WINHTTP_QUERY_URI
的标志可以查询究竟是什么 HTTP 标头?在阅读了它的描述之后,我觉得这个标志应该被用来获取在WinHttpOpenRequest函数中指定的请求的 URI。然而,下面的程序给了我一个错误代码 12019 (如果我取消注释两个注释行,则为 12150)。ERROR_INTERNET_INCORRECT_HANDLE_STATE
ERROR_HTTP_HEADER_NOT_FOUND
#include <cstdio>
#include <windows.h>
#include <winhttp.h>
#pragma comment(lib, "winhttp.lib")
int main()
{
HINTERNET hSession = WinHttpOpen(nullptr, WINHTTP_ACCESS_TYPE_NO_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);
HINTERNET hConnect = WinHttpConnect(hSession, L"www.ietf.org", INTERNET_DEFAULT_HTTP_PORT, 0);
HINTERNET hRequest = WinHttpOpenRequest(hConnect, L"GET", L"/rfc/rfc2616.txt", nullptr, WINHTTP_NO_REFERER, nullptr, 0);
//WinHttpSendRequest(hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0, WINHTTP_NO_REQUEST_DATA, 0, 0, 0);
//WinHttpReceiveResponse(hRequest, 0);
wchar_t url[1024] = {};
DWORD url_size = sizeof(url);
auto success = WinHttpQueryHeaders(hRequest, WINHTTP_QUERY_URI, WINHTTP_HEADER_NAME_BY_INDEX, url, &url_size, WINHTTP_NO_HEADER_INDEX);
auto error_code = GetLastError();
wprintf(L"success=%d error_code=%u url=%s", success, error_code, url);
WinHttpCloseHandle(hRequest);
WinHttpCloseHandle(hConnect);
WinHttpCloseHandle(hSession);
}
PS 是的,我知道我可以使用WinHttpQueryOption
and获取请求 URI WINHTTP_OPTION_URL
,无需指出这一点。
编辑。根据下面的 Captain Obvlious 回答添加标志(如果确实应该返回请求 URI WINHTTP_QUERY_FLAG_REQUEST_HEADERS
,这完全有意义)并没有太大区别:现在有或没有and调用函数都会产生错误代码 12150 。 WINHTTP_QUERY_URI
WinHttpSendRequest
WinHttpReceiveResponse
WinHttpQueryHeaders
ERROR_HTTP_HEADER_NOT_FOUND