使用 WinHTTP 通过 CONNECT 和 POST 发送用户代理?

发布于 2024-08-24 17:37:51 字数 2328 浏览 6 评论 0原文

我正在尝试使用 WinHttp POST 到安全站点,但遇到了 User-Agent 标头未与 CONNECT 一起发送的问题。

我正在使用 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)连接到我的服务器,并进行 POST。这是有效的,但是当我检查代理日志时,用户代理字符串(“WinHTTP AutoProxy Sample/1.0”)没有作为 CONNECT 的一部分发送。然而,它作为 POST 的一部分发送。

有人可以告诉我如何更改此代码以在 CONNECT 和 POST 期间发送 User-Agent 标头吗?

编辑添加:我们仅在 Windows 7 上观察到此问题。如果我们在 Windows Vista 机器上运行相同的代码,我们可以看到正在发送的 User-Agent 标头在连接上。

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.

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 );

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.

Could someone please tell me how I can change this code to have the User-Agent header sent during both the CONNECT and POST?

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

〃安静 2024-08-31 17:37:51

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文