Dela via


WinHTTP AutoProxy Functions

WinHTTP implementerar WPAD-protokollet med hjälp av funktionen WinHttpGetProxyForUrl tillsammans med två stödverktygsfunktioner, WinHttpDetectAutoProxyConfigUrl och WinHttpGetIEProxyConfigForCurrentUser.

AutoProxy-stöd är inte helt integrerat i HTTP-stacken i WinHTTP. Innan du skickar en begäran måste programmet anropa WinHttpGetProxyForUrl för att hämta namnet på en proxyserver och sedan anropa WinHttpSetOption med hjälp av WINHTTP_OPTION_PROXY för att ange proxykonfigurationen på WinHTTP-begärandehandtaget som skapats av WinHttpOpenRequest.

Funktionen WinHttpGetProxyForUrl kan köra alla tre stegen i WPAD-protokollet som beskrivs i föregående översikt: (1) identifiera PAC-URL:en, (2) ladda ned PAC-skriptfilen, (3) köra skriptkoden och returnera proxykonfigurationen i en WINHTTP_PROXY_INFO struktur. Om programmet i förväg vet PAC-URL:en kan det också ange detta för att WinHttpGetProxyForUrl.

I följande exempelkod används autoproxy. Den konfigurerar en HTTP GET-begäran genom att först skapa anslutnings- och begärandehandtagen för WinHTTP-sessionen. Anropet WinHttpOpen anger WINHTTP_ACCESS_TYPE_NO_PROXY för den inledande proxykonfigurationen för att ange att begäranden skickas direkt till målservern som standard. Med autoproxy anger den sedan proxykonfigurationen direkt på begärandehandtaget.

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

I den angivna exempelkoden instruerar anropet till WinHttpGetProxyForUrl funktionen att identifiera den automatiska proxykonfigurationsfilen automatiskt genom att ange flaggan WINHTTP_AUTOPROXY_AUTO_DETECT i WINHTTP_AUTOPROXY_OPTIONS-strukturen. Användning av flaggan WINHTTP_AUTOPROXY_AUTO_DETECT kräver att koden anger en eller båda flaggorna för automatisk identifiering (WINHTTP_AUTO_DETECT_TYPE_DHCP, WINHTTP_AUTO_DETECT_TYPE_DNS_A). Exempelkoden använder funktionen för automatisk identifiering av WinHttpGetProxyForUrl eftersom PAC-URL:en inte är känd i förväg. Om det inte går att hitta en PAC-URL i nätverket i det här scenariot misslyckas WinHttpGetProxyForUrl (GetLastError returnerar ERROR_WINHTTP_AUTODETECTION_FAILED).

Om PAC-URL:en är känd i förväg

Om programmet känner till PAC-URL:en kan det ange den i WINHTTP_AUTOPROXY_OPTIONS-strukturen och konfigurera WinHttpGetProxyForUrl- för att hoppa över fasen för automatisk identifiering.

Om en PAC-fil till exempel är tillgänglig i det lokala nätverket på URL:en , "https://InternalSite/proxy-config.pac", skulle anropet till WinHttpGetProxyForUrl titta på följande.

//
// 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 ) )
{
  //...

Om WINHTTP_AUTOPROXY_OPTIONS-strukturen anger både WINHTTP_AUTOPROXY_AUTO_DETECT- och WINHTTP_AUTOPROXY_CONFIG_URL flaggor (och anger flaggor för automatisk detction och en url för automatisk konfiguration) WinHttpGetProxyForUrl första försöket till automatisk identifiering och sedan, om automatisk identifiering inte hittar en PAC-URL, "faller tillbaka" till den automatiska konfigurations-URL som tillhandahålls av programmet.

Funktionen WinHttpDetectAutoProxyConfigUrl

Funktionen WinHttpDetectAutoProxyConfigUrl implementerar en delmängd av WPAD-protokollet: den försöker automatiskt identifiera URL:en för proxyfilen för automatisk konfiguration, utan att ladda ned eller köra PAC-filen. Den här funktionen är användbar i särskilda situationer där ett webbklientprogram måste hantera nedladdningen och körningen av själva PAC-filen.

Funktionen WinHttpGetIEProxyConfigForCurrentUser

Funktionen WinHttpGetIEProxyConfigForCurrentUser returnerar de aktuella proxyinställningarna för Internet Explorer för den aktuella aktiva nätverksanslutningen, utan att anropa till "WinInet.dll". Den här funktionen är bara användbar när den anropas i en process som körs under en interaktiv användarkontoidentitet, eftersom ingen Internet Explorer-proxykonfiguration sannolikt kommer att vara tillgänglig annars. Det skulle till exempel inte vara användbart att anropa den här funktionen från en ISAPI-DLL som körs i IIS-tjänstprocessen. Mer information och ett scenario där ett WinHTTP-baserat program skulle använda WinHttpGetIEProxyConfigForCurrentUserfinns i Discovery Without an Auto-Config File.