Send User-Agent through CONNECT and POST with WinHTTP?

Posted by Duncan Bayne on Stack Overflow See other posts from Stack Overflow or by Duncan Bayne
Published on 2010-03-07T14:22:32Z Indexed on 2010/03/09 2:06 UTC
Read the original article Hit count: 697

Filed under:
|
|
|
|

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.

© Stack Overflow or respective owner

Related posts about winhttp

Related posts about proxy