Partilhar via


Funções WinHTTP AutoProxy

WinHTTP implementa o protocolo WPAD usando o WinHttpGetProxyForUrl função juntamente com duas funções de utilitário de suporte, WinHttpDetectAutoProxyConfigUrl e WinHttpGetIEProxyConfigForCurrentUser.

O suporte a AutoProxy não está totalmente integrado na pilha HTTP no WinHTTP. Antes de enviar uma solicitação, o aplicativo deve chamar WinHttpGetProxyForUrl para obter o nome de um servidor proxy e, em seguida, chamar WinHttpSetOption usando WINHTTP_OPTION_PROXY para definir a configuração de proxy no identificador de solicitação WinHTTP criado por WinHttpOpenRequest.

O função WinHttpGetProxyForUrl pode executar todas as três etapas do protocolo WPAD descritas na visão geral anterior: (1) descobrir a URL da PAC, (2) baixar o arquivo de script PAC, (3) executar o código do script e retornar a configuração do proxy em uma estrutura WINHTTP_PROXY_INFO. Opcionalmente, se o aplicativo souber com antecedência a URL da PAC, ele poderá especificar isso para WinHttpGetProxyForUrl.

O código de exemplo a seguir usa autoproxy. Ele configura uma solicitação HTTP GET criando primeiro os identificadores de conexão e solicitação de sessão WinHTTP. A chamada WinHttpOpen especifica WINHTTP_ACCESS_TYPE_NO_PROXY para a configuração de proxy inicial, para indicar que as solicitações são enviadas diretamente para o servidor de destino por padrão. Usando o autoproxy, ele define a configuração do proxy diretamente no identificador de solicitação.

  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) );
  
//
// Create the WinHTTP session.
//
  hHttpSession = WinHttpOpen( L"WinHTTP AutoProxy Sample/1.0",
                              WINHTTP_ACCESS_TYPE_NO_PROXY,
                              WINHTTP_NO_PROXY_NAME,
                              WINHTTP_NO_PROXY_BYPASS,
                              0 );
  
// Exit if WinHttpOpen failed.
  if( !hHttpSession )
    goto Exit;
  
//
// Create the WinHTTP connect handle.
//
  hConnect = WinHttpConnect( hHttpSession,
                             L"www.microsoft.com",
                             INTERNET_DEFAULT_HTTP_PORT,
                             0 );
  
// Exit if WinHttpConnect failed.
  if( !hConnect )
    goto Exit;
  
//
// Create the HTTP request handle.
//
  hRequest = WinHttpOpenRequest( hConnect,
                                 L"GET",
                                 L"ms.htm",
                                 L"HTTP/1.1",
                                 WINHTTP_NO_REFERER,
                                 WINHTTP_DEFAULT_ACCEPT_TYPES,
                                 0 );
  
// Exit if WinHttpOpenRequest failed.
  if( !hRequest )
    goto Exit;
  
//
// Set up the autoproxy call.
//

// Use auto-detection because the Proxy 
// Auto-Config URL is not known.
  AutoProxyOptions.dwFlags = WINHTTP_AUTOPROXY_AUTO_DETECT;

// Use DHCP and DNS-based auto-detection.
  AutoProxyOptions.dwAutoDetectFlags = 
                             WINHTTP_AUTO_DETECT_TYPE_DHCP |
                             WINHTTP_AUTO_DETECT_TYPE_DNS_A;

// If obtaining the PAC script requires NTLM/Negotiate
// authentication, then automatically supply the client
// domain credentials.
  AutoProxyOptions.fAutoLogonIfChallenged = TRUE;

//
// Call WinHttpGetProxyForUrl with our target URL. If 
// auto-proxy succeeds, then set the proxy info on the 
// request handle. If auto-proxy fails, ignore the error 
// and attempt to send the HTTP request directly to the 
// target server (using the default WINHTTP_ACCESS_TYPE_NO_PROXY 
// configuration, which the requesthandle will inherit 
// from the session).
//
  if( WinHttpGetProxyForUrl( hHttpSession,
                             L"https://www.microsoft.com/ms.htm",
                             &AutoProxyOptions,
                             &ProxyInfo))
  {
  // A proxy configuration was found, set it on the
  // request handle.
    
    if( !WinHttpSetOption( hRequest, 
                           WINHTTP_OPTION_PROXY,
                           &ProxyInfo,
                           cbProxyInfoSize ) )
    {
      // Exit if setting the proxy info failed.
      goto Exit;
    }
  }

//
// Send the request.
//
  if( !WinHttpSendRequest( hRequest,
                           WINHTTP_NO_ADDITIONAL_HEADERS,
                           0,
                           WINHTTP_NO_REQUEST_DATA,
                           0,
                           0,
                           NULL ) )
  {
    // Exit if WinHttpSendRequest failed.
    goto Exit;
  }

//
// Wait for the response.
//

  if( !WinHttpReceiveResponse( hRequest, NULL ) )
    goto Exit;

//
// A response has been received, then process it.
// (omitted)
//


  Exit:
  //
  // Clean up the WINHTTP_PROXY_INFO structure.
  //
    if( ProxyInfo.lpszProxy != NULL )
      GlobalFree(ProxyInfo.lpszProxy);

    if( ProxyInfo.lpszProxyBypass != NULL )
      GlobalFree( ProxyInfo.lpszProxyBypass );

  //
  // Close the WinHTTP handles.
  //
    if( hRequest != NULL )
      WinHttpCloseHandle( hRequest );
  
    if( hConnect != NULL )
      WinHttpCloseHandle( hConnect );
  
    if( hHttpSession != NULL )
      WinHttpCloseHandle( hHttpSession );

No código de exemplo fornecido, a chamada para WinHttpGetProxyForUrl instrui a função a descobrir o arquivo de configuração automática de proxy automaticamente, especificando o sinalizador de WINHTTP_AUTOPROXY_AUTO_DETECT na estrutura WINHTTP_AUTOPROXY_OPTIONS. O uso do sinalizador WINHTTP_AUTOPROXY_AUTO_DETECT requer que o código especifique um ou ambos os sinalizadores de deteção automática (WINHTTP_AUTO_DETECT_TYPE_DHCP, WINHTTP_AUTO_DETECT_TYPE_DNS_A). O código de exemplo usa o recurso de deteção automática de WinHttpGetProxyForUrl porque a URL da PAC não é conhecida antecipadamente. Se uma URL PAC não puder ser localizada na rede nesse cenário, WinHttpGetProxyForUrl falhar (GetLastError retorna ERROR_WINHTTP_AUTODETECTION_FAILED).

Se o URL da PAC for conhecido com antecedência

Se o aplicativo souber a URL da PAC, ele poderá especificá-la na estrutura WINHTTP_AUTOPROXY_OPTIONS e configurar WinHttpGetProxyForUrl para ignorar a fase de deteção automática.

Por exemplo, se um arquivo PAC estiver disponível na rede local na URL, "https://InternalSite/proxy-config.pac", a chamada para WinHttpGetProxyForUrl terá a seguinte aparência.

//
// Set up the autoproxy call.
//

// The proxy auto-config URL is known. Auto-detection
// is not required.
  AutoProxyOptions.dwFlags = WINHTTP_AUTOPROXY_CONFIG_URL;

// Set the proxy auto-config URL.
  AutoProxyOptions. lpszAutoConfigUrl =  L"https://InternalSite/proxy-config.pac";

// If obtaining the PAC script requires NTLM/Negotiate
// authentication, then automatically supply the client
// domain credentials.
  AutoProxyOptions.fAutoLogonIfChallenged = TRUE;

//
// Call WinHttpGetProxyForUrl with our target URL. If auto-proxy
// succeeds, then set the proxy info on the request handle.
// If auto-proxy fails, ignore the error and attempt to send the
// HTTP request directly to the target server (using the default
// WINHTTP_ACCESS_TYPE_NO_PROXY configuration, which the request
// handle will inherit from the session).
//
  if( WinHttpGetProxyForUrl( hHttpSession,
                             L"https://www.microsoft.com/ms.htm",
                             &AutoProxyOptions,
                             &ProxyInfo ) )
{
  //...

Se a estrutura WINHTTP_AUTOPROXY_OPTIONS especificar sinalizadores WINHTTP_AUTOPROXY_AUTO_DETECT e WINHTTP_AUTOPROXY_CONFIG_URL (e especificar sinalizadores de auto-detction e uma URL de configuração automática), WinHttpGetProxyForUrl primeiro tentar a deteção automática e, em seguida, se a deteção automática falhar ao localizar uma URL PAC, "retornará" para a URL de configuração automática fornecida pelo aplicativo.

A função WinHttpDetectAutoProxyConfigUrl

O função WinHttpDetectAutoProxyConfigUrl implementa um subconjunto do protocolo WPAD: ele tenta detetar automaticamente a URL para o arquivo de configuração automática de proxy, sem baixar ou executar o arquivo PAC. Esta função é útil em situações especiais em que um aplicativo cliente da Web deve lidar com o download e a execução do próprio arquivo PAC.

A função WinHttpGetIEProxyConfigForCurrentUser

A funçãoWinHttpGetIEProxyConfigForCurrentUser retorna as configurações de proxy do Internet Explorer do usuário atual para a conexão de rede ativa atual, sem chamar "WinInet.dll". Essa função só é útil quando chamada dentro de um processo que está sendo executado sob uma identidade de conta de usuário interativa, porque nenhuma configuração de proxy do Internet Explorer provavelmente estará disponível de outra forma. Por exemplo, não seria útil chamar essa função de uma DLL ISAPI em execução no processo de serviço do IIS. Para obter mais informações e um cenário no qual um aplicativo baseado em WinHTTP usaria WinHttpGetIEProxyConfigForCurrentUser , consulte Discovery Without an Auto-Config File.