使用WinHTTP通过CONNECT和POST发送用户代理? [英] Send User-Agent through CONNECT and POST with WinHTTP?
问题描述
我正在尝试使用WinHttp发布到安全站点,并且遇到了一个问题,其中User-Agent标头没有与CONNECT一起发送。
I'm trying to POST to a secure site using WinHttp, and running into a problem where the User-Agent header isn't being sent along with the CONNECT.
我正在使用MSDN中经过轻微修改的代码示例:
I am using a lightly-modified code sample from MSDN:
HINTERNET hHttpSession = NULL;
HINTERNET hConnect = NULL;
HINTERNET hRequest = NULL;
WINHTTP_AUTOPROXY_OPTIONS AutoProxyOptions;
WINHTTP_PROXY_INFO ProxyInfo;
DWORD cbProxyInfoSize = sizeof(ProxyInfo);
ZeroMemory( &AutoProxyOptions, sizeof(AutoProxyOptions) );
ZeroMemory( &ProxyInfo, sizeof(ProxyInfo) );
hHttpSession = WinHttpOpen(L"WinHTTP AutoProxy Sample/1.0",
WINHTTP_ACCESS_TYPE_NO_PROXY,
WINHTTP_NO_PROXY_NAME,
WINHTTP_NO_PROXY_BYPASS,
0);
if(!hHttpSession)
goto Exit;
hConnect = WinHttpConnect( hHttpSession,
L"server.com",
INTERNET_DEFAULT_HTTPS_PORT,
0 );
if( !hConnect )
goto Exit;
hRequest = WinHttpOpenRequest(hConnect, L"POST", L"/resource", NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, WINHTTP_FLAG_SECURE );
if( !hRequest )
goto Exit;
WINHTTP_PROXY_INFO proxyInfo;
proxyInfo.dwAccessType = WINHTTP_ACCESS_TYPE_NAMED_PROXY;
proxyInfo.lpszProxy = L"192.168.1.2:3199";
proxyInfo.lpszProxyBypass = L"";
WinHttpSetOption(hHttpSession,
WINHTTP_OPTION_PROXY,
&proxyInfo,
sizeof(proxyInfo));
WinHttpSetCredentials(hRequest, WINHTTP_AUTH_TARGET_PROXY, WINHTTP_AUTH_SCHEME_BASIC, L"proxyuser", L"proxypass", NULL);
if( !WinHttpSendRequest(hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0, "content", 7, 7, 0))
{
goto Exit;
}
if(!WinHttpReceiveResponse(hRequest, NULL))
goto Exit;
/* handle result */
Exit:
if( ProxyInfo.lpszProxy != NULL )
GlobalFree(ProxyInfo.lpszProxy);
if( ProxyInfo.lpszProxyBypass != NULL )
GlobalFree( ProxyInfo.lpszProxyBypass );
if( hRequest != NULL )
WinHttpCloseHandle( hRequest );
if( hConnect != NULL )
WinHttpCloseHandle( hConnect );
if( hHttpSession != NULL )
WinHttpCloseHandle( hHttpSession );
此操作是通过192.168.1.2:3199处经过身份验证的代理连接到我的服务器,并进行一个帖子。这可行,但是当我检查代理日志时,未将用户代理字符串( WinHTTP AutoProxy Sample / 1.0)作为CONNECT的一部分发送。但是,它是作为POST的一部分发送的。
What this does is connect to my server through an authenticated proxy at 192.168.1.2:3199, and make a POST. This works, but when I examine the proxy logs the User-Agent string ("WinHTTP AutoProxy Sample/1.0") is not being sent as part of the CONNECT. It is however sent as part of the POST.
有人可以告诉我如何更改此代码,以在两次都发送User-Agent标头 CONNECT和POST吗?
Could someone please tell me how I can change this code to have the User-Agent header sent during both the CONNECT and POST?
编辑后添加: 我们仅在Windows 7上观察到此问题如果在Windows Vista机器上运行相同的代码,则可以看到User-Agent标头是通过CONNECT发送的。
Edited to add: we are observing this problem only on Windows 7. If we run the same code on a Windows Vista box, we can see the User-Agent header being sent on CONNECT.
推荐答案
邓肯(Duncan)–根据WinHTTP团队的说法,这是Windows 7中引入的行为更改。目前,WinHTTP中没有针对此问题的解决方法。
Duncan-- Per the WinHTTP team, this was a behavioral change introduced in Windows 7. At present, there is no workaround for this issue in WinHTTP.
这篇关于使用WinHTTP通过CONNECT和POST发送用户代理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!