我在使用WinHTTP进行NTLM身份验证时遇到了一些问题。虽然监视wireshark中的流量,但我可以看到正在进行的请求,但客户端从不发送必需的身份验证标头,因此每次服务器响应401验证所需的消息时。
我并不支持任何直接与主机建立IP连接的代理服务器,因此不应该成为问题。
这是我的代码:非常感谢帮助。
hSession = WinHttpOpen(L"Connection/1.0",
WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);
hConnect = WinHttpConnect(hSession, L"connect", INTERNET_DEFAULT_HTTP_PORT, 0);
hRequest = WinHttpOpenRequest(hConnect, L"GET", url, NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, NULL);
WinHttpSetOption (hRequest, WINHTTP_OPTION_AUTOLOGON_POLICY, 0, 0);
int i = 0;
while(!bDone)
{
i++;
WinHttpSetCredentials(hRequest, WINHTTP_AUTH_TARGET_SERVER, WINHTTP_AUTH_SCHEME_NTLM, NULL, NULL, NULL);
//Send the request
bResults = WinHttpSendRequest(hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0, WINHTTP_NO_REQUEST_DATA, 0, 0, 0);
//End the request
if(bResults)
bResults = WinHttpReceiveResponse(hRequest, NULL);
if( bResults )
bResults = WinHttpQueryHeaders(hRequest, WINHTTP_QUERY_STATUS_CODE | WINHTTP_QUERY_FLAG_NUMBER, NULL, &dwStatusCode, &dwSize, NULL );
if(bResults)
{
switch(dwStatusCode)
{
case 401:
//Requres auth
newFile << "401 Auth Required\n";
WinHttpSetCredentials(hRequest, WINHTTP_AUTH_TARGET_SERVER, WINHTTP_AUTH_SCHEME_NTLM, NULL, NULL, NULL);
break;
default:
newFile << dwStatusCode << " unknown\n";
break;
}
}
if(i >= 30)
bDone = TRUE;
}