WinHTTP AutoProxy-Funktionen

WinHTTP implementiert das WPAD-Protokoll mithilfe der WinHttpGetProxyForUrl-Funktion zusammen mit zwei unterstützenden Hilfsfunktionen, WinHttpDetectAutoProxyConfigUrl und WinHttpGetIEProxyConfigForCurrentUser.

Die AutoProxy-Unterstützung ist nicht vollständig in den HTTP-Stapel in WinHTTP integriert. Vor dem Senden einer Anforderung muss die Anwendung WinHttpGetProxyForUrl aufrufen, um den Namen eines Proxyservers abzurufen und dann WinHttpSetOption mithilfe von WINHTTP_OPTION_PROXY aufzurufen, um die Proxykonfiguration auf dem WinHTTP-Anforderungshandle festzulegen, das von WinHttpOpenRequest erstellt wurde.

Die WinHttpGetProxyForUrl-Funktion kann alle drei Schritte des in der vorherigen Übersicht beschriebenen WPAD-Protokolls ausführen: (1) entdecken Sie die PAC-URL, (2) laden Sie die PAC-Skriptdatei herunter, (3) führen den Skriptcode aus, und geben Sie die Proxykonfiguration in einer WINHTTP_PROXY_INFO Struktur zurück. Optional, wenn die Anwendung im Voraus die PAC-URL kennt, kann dies für WinHttpGetProxyForUrl angegeben werden.

Im folgenden Beispielcode wird autoproxy verwendet. Sie richtet eine HTTP GET-Anforderung ein, indem sie zuerst die WinHTTP-Sitzungsverbindungs- und Anforderungshandles erstellt. Der WinHttpOpen-Aufruf gibt WINHTTP_ACCESS_TYPE_NO_PROXY für die anfängliche Proxykonfiguration an, um anzugeben, dass Anforderungen standardmäßig direkt an den Zielserver gesendet werden. Mithilfe von Autoproxy wird dann die Proxykonfiguration direkt auf dem Anforderungshandpunkt festgelegt.

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

Im bereitgestellten Beispielcode weist der Aufruf von WinHttpGetProxyForUrl die Funktion an, die Proxy-Autokonfigurationsdatei automatisch zu ermitteln, indem die WINHTTP_AUTOPROXY_AUTO_DETECT Flag in der WINHTTP_AUTOPROXY_OPTIONS Struktur angegeben wird. Die Verwendung des WINHTTP_AUTOPROXY_AUTO_DETECT-Flags erfordert den Code, um eine oder beide der automatischen Erkennungskennzeichnungen (WINHTTP_AUTO_DETECT_TYPE_DHCP, WINHTTP_AUTO_DETECT_TYPE_DNS_A) anzugeben. Der Beispielcode verwendet das Feature für die automatische Erkennung von WinHttpGetProxyForUrl , da die PAC-URL im Voraus nicht bekannt ist. Wenn sich eine PAC-URL nicht im Netzwerk in diesem Szenario befindet, schlägt WinHttpGetProxyForUrl fehl (GetLastError gibt ERROR_WINHTTP_AUTODETECTION_FAILED zurück).

Wenn die PAC-URL im Voraus bekannt ist

Wenn die Anwendung die PAC-URL kennt, kann sie sie in der WINHTTP_AUTOPROXY_OPTIONS Struktur angeben und WinHttpGetProxyForUrl so konfigurieren, dass die automatische Erkennungsphase übersprungen wird.

Wenn beispielsweise eine PAC-Datei im lokalen Netzwerk unter der URL "https://InternalSite/proxy-config.pac" verfügbar ist, würde der Aufruf von WinHttpGetProxyForUrl wie folgt aussehen.

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

Wenn die WINHTTP_AUTOPROXY_OPTIONS-Struktur sowohl WINHTTP_AUTOPROXY_AUTO_DETECT als auch WINHTTP_AUTOPROXY_CONFIG_URL Flags angibt (und automatisch detction flags und eine Autokonfigurations-URL angibt), versucht WinHttpGetProxyForUrl zunächst die automatische Erkennung und dann, wenn die automatische Erkennung keine PAC-URL finden kann, "fällt zurück" auf die von der Anwendung bereitgestellte Autokonfigurations-URL.

Die WinHttpDetectAutoProxyConfigUrl-Funktion

Die WinHttpDetectAutoProxyConfigUrl-Funktion implementiert eine Teilmenge des WPAD-Protokolls: Es versucht, die URL für die Proxy-Autokonfigurationsdatei automatisch zu erkennen, ohne die PAC-Datei herunterzuladen oder auszuführen. Diese Funktion ist in speziellen Situationen nützlich, in denen eine Webanwendung den Download und die Ausführung der PAC-Datei selbst verarbeiten muss.

Die WinHttpGetIEProxyConfigForCurrentUser-Funktion

Die WinHttpGetIEProxyConfigForCurrentUser-Funktion gibt die aktuellen Internet Explorer-Proxyeinstellungen für die aktuelle aktive Netzwerkverbindung zurück, ohne "WinInet.dll" aufrufen zu müssen. Diese Funktion ist nur nützlich, wenn sie innerhalb eines Prozesses aufgerufen wird, der unter einer interaktiven Benutzerkontoidentität ausgeführt wird, da keine Internet Explorer-Proxykonfiguration sonst verfügbar ist. Beispielsweise wäre es nicht hilfreich, diese Funktion aus einer ISAPI-DLL aufzurufen, die im IIS-Dienstprozess ausgeführt wird. Weitere Informationen und ein Szenario, in dem eine winHTTP-basierte Anwendung WinHttpGetIEProxyConfigForCurrentUser verwenden würde, finden Sie unter Discovery Without an Auto-Config File.