共用方式為


WinHTTP AutoProxy 函式

WinHTTP 會使用WinHttpGetProxyForUrl函式來實作 WPAD 通訊協定,以及兩個支援的公用程式函式:WinHttpDetectAutoProxyConfigUrl 和 WinHttpGetIEProxyConfigForCurrentUser

AutoProxy 支援未完全整合到 WinHTTP 中的 HTTP 堆疊中。 傳送要求之前,應用程式必須呼叫WinHttpGetProxyForUrl以取得 Proxy 伺服器的名稱,然後使用WINHTTP_OPTION_PROXY呼叫WinHttpSetOption,在WinHttpOpenRequest所建立的 WinHTTP 要求控制碼上設定 Proxy 組態。

WinHttpGetProxyForUrl函式可以執行上一個概觀中所述 WPAD 通訊協定的所有三個步驟: (1) 探索 PAC URL, (2) 下載 PAC 腳本檔案, (3) 執行腳本程式碼,並在WINHTTP_PROXY_INFO結構中傳回 Proxy 組態。 選擇性地,如果應用程式事先知道 PAC URL,它可以指定給 WinHttpGetProxyForUrl

下列範例程式碼使用 autoproxy。 它會先建立 WinHTTP 會話連線和要求控制碼來設定 HTTP GET 要求。 WinHttpOpen呼叫會指定初始 Proxy 組態的WINHTTP_ACCESS_TYPE_NO_PROXY,以指出要求預設會直接傳送至目標伺服器。 使用 autoproxy,然後直接在要求控制碼上設定 Proxy 組態。

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

在提供的範例程式碼中,對 WinHttpGetProxyForUrl的呼叫會指示函式在WINHTTP_AUTOPROXY_OPTIONS結構中指定WINHTTP_AUTOPROXY_AUTO_DETECT旗標,以自動探索 Proxy 自動設定檔。 使用 WINHTTP_AUTOPROXY_AUTO_DETECT 旗標需要程式碼指定一或兩個自動偵測旗標, (WINHTTP_AUTO_DETECT_TYPE_DHCPWINHTTP_AUTO_DETECT_TYPE_DNS_A) 。 範例程式碼會使用 WinHttpGetProxyForUrl 的自動偵測功能,因為事先不知道 PAC URL。 如果 PAC URL 無法在此案例中位於網路上, WinHttpGetProxyForUrl 會失敗, (GetLastError 會傳回 ERROR_WINHTTP_AUTODETECTION_FAILED) 。

如果事先知道 PAC URL

如果應用程式知道 PAC URL,它可以在 WINHTTP_AUTOPROXY_OPTIONS 結構中指定它,並將 WinHttpGetProxyForUrl 設定為略過自動偵測階段。

例如,如果在 URL 的區域網路上使用 PAC 檔案,則 https://InternalSite/proxy-config.pac" 呼叫 WinHttpGetProxyForUrl 會如下所示。

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

如果 WINHTTP_AUTOPROXY_OPTIONS 結構同時指定 WINHTTP_AUTOPROXY_AUTO_DETECTWINHTTP_AUTOPROXY_CONFIG_URL 旗標 (,並指定自動解碼旗標和自動設定 URL) , WinHttpGetProxyForUrl 會先嘗試自動偵測,然後,如果自動偵測找不到 PAC URL,則「回復」到應用程式所提供的自動設定 URL。

WinHttpDetectAutoProxyConfigUrl 函式

WinHttpDetectAutoProxyConfigUrl函式會實作 WPAD 通訊協定的子集:它會嘗試自動偵測 Proxy 自動設定檔的 URL,而不需下載或執行 PAC 檔案。 在 Web 用戶端應用程式必須處理 PAC 檔案本身的下載和執行時,此函式很有用。

WinHttpGetIEProxyConfigForCurrentUser 函式

WinHttpGetIEProxyConfigForCurrentUser函式會傳回目前作用中網路連線的目前使用者 Internet Explorer Proxy 設定,而不需呼叫 「WinInet.dll」。 只有在以互動式使用者帳戶身分識別執行的進程內呼叫時,此函式才有用,因為可能沒有 Internet Explorer Proxy 組態可供使用, 例如,從 IIS 服務進程中執行的 ISAPI DLL 呼叫此函式並不實用。 如需詳細資訊和以 WinHTTP 為基礎的應用程式會使用 WinHttpGetIEProxyConfigForCurrentUser的案例,請參閱 沒有自動設定檔的探索