WinHTTP AutoProxy-Funktionen

WinHTTP implementiert das WPAD-Protokoll mithilfe der WinHttpGetProxyForUrl-Funktion zusammen mit zwei unterstützenden Hilfsprogrammfunktionen: 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 aufrufen, um die Proxykonfiguration für das winHTTP-Anforderungshandle festzulegen, das von WinHttpOpenRequest erstellt wurde.

Die WinHttpGetProxyForUrl-Funktion kann alle drei Schritte des WPAD-Protokolls ausführen, die in der vorherigen Übersicht beschrieben sind: (1) Die PAC-URL ermitteln, (2) die PAC-Skriptdatei herunterladen, (3) den Skriptcode ausführen und die Proxykonfiguration in einer WINHTTP_PROXY_INFO-Struktur zurückgeben. Wenn die Anwendung die PAC-URL im Voraus kennt, kann sie dies in WinHttpGetProxyForUrl angeben.

Im folgenden Beispielcode wird autoproxy verwendet. Es richtet eine HTTP GET-Anforderung ein, indem zuerst die WinHTTP-Sitzungsverbindungs- und Anforderungshandles erstellt werden. 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 im Anforderungshandle 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 das flag WINHTTP_AUTOPROXY_AUTO_DETECT in der WINHTTP_AUTOPROXY_OPTIONS-Struktur angegeben wird. Die Verwendung des WINHTTP_AUTOPROXY_AUTO_DETECT-Flags erfordert, dass der Code eines oder beide der Flags für die automatische Erkennung (WINHTTP_AUTO_DETECT_TYPE_DHCP, WINHTTP_AUTO_DETECT_TYPE_DNS_A) angibt. Der Beispielcode verwendet das Feature für die automatische Erkennung von WinHttpGetProxyForUrl , da die PAC-URL im Voraus nicht bekannt ist. Wenn in diesem Szenario keine PAC-URL im Netzwerk gefunden werden kann, 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 Phase der automatischen Erkennung ü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 Flags für die automatische Deskription und eine URL für die automatische Konfiguration angibt), versucht WinHttpGetProxyForUrl zunächst die automatische Erkennung, und wenn die automatische Erkennung eine PAC-URL nicht findet, "greift" auf die von der Anwendung bereitgestellte URL für die automatische Konfiguration zurück.

Die WinHttpDetectAutoProxyConfigUrl-Funktion

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

Die WinHttpGetIEProxyConfigForCurrentUser-Funktion

Die WinHttpGetIEProxyConfigForCurrentUser-Funktion gibt den aktuellen Internet Explorer Proxyeinstellungen für die aktuelle aktive Netzwerkverbindung zurück, ohne "WinInet.dll" aufzurufen. Diese Funktion ist nur nützlich, wenn sie in einem Prozess aufgerufen wird, der unter einer interaktiven Benutzerkontoidentität ausgeführt wird, da andernfalls wahrscheinlich keine Internet-Explorer Proxykonfiguration verfügbar ist. Beispielsweise wäre es nicht sinnvoll, 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 verwendet, finden Sie unter Ermittlung ohne Auto-Config-Datei.