다음을 통해 공유


WinHTTP AutoProxy 함수

WinHTTP는 WinHttpGetProxyForUrl 함수와 두 가지 지원 유틸리티 함수 인 WinHttpDetectAutoProxyConfigUrlWinHttpGetIEProxyConfigForCurrentUser를 사용하여 WPAD 프로토콜을 구현합니다.

AutoProxy 지원은 WinHTTP의 HTTP 스택에 완전히 통합되지 않습니다. 요청을 보내기 전에 애플리케이션은 WinHttpGetProxyForUrl을 호출하여 프록시 서버의 이름을 가져온 다음, WINHTTP_OPTION_PROXY 사용하여 WinHttpSetOption을 호출하여 WinHttpOpenRequest에서 만든 WinHTTP 요청 핸들에서 프록시 구성을 설정해야 합니다.

WinHttpGetProxyForUrl 함수는 이전 개요에 설명된 WPAD 프로토콜의 세 단계를 모두 실행할 수 있습니다. (1) PAC URL 검색, (2) PAC 스크립트 파일 다운로드, (3) 스크립트 코드를 실행하고 WINHTTP_PROXY_INFO 구조에서 프록시 구성을 반환합니다. 필요에 따라 애플리케이션이 PAC URL을 미리 알고 있는 경우 WinHttpGetProxyForUrl로 지정할 수 있습니다.

다음 예제 코드는 autoproxy를 사용합니다. 먼저 WinHTTP 세션 연결 및 요청 핸들을 만들어 HTTP GET 요청을 설정합니다. WinHttpOpen 호출은 요청이 기본적으로 대상 서버로 직접 전송됨을 나타내기 위해 초기 프록시 구성에 대한 WINHTTP_ACCESS_TYPE_NO_PROXY 지정합니다. autoproxy를 사용하여 요청 핸들에서 직접 프록시 구성을 설정합니다.

  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 플래그를 지정하여 프록시 자동 구성 파일을 자동으로 검색하도록 함수에 지시합니다. WINHTTP_AUTOPROXY_AUTO_DETECT 플래그를 사용하려면 코드에서 자동 검색 플래그(WINHTTP_AUTO_DETECT_TYPE_DHCP, WINHTTP_AUTO_DETECT_TYPE_DNS_A) 중 하나 또는 둘 다를 지정해야 합니다. 예제 코드는 PAC URL을 미리 알 수 없 으므로 WinHttpGetProxyForUrl 의 자동 검색 기능을 사용합니다. 이 시나리오에서 PAC URL을 네트워크에 배치할 수 없는 경우 WinHttpGetProxyForUrl 이 실패합니다(GetLastErrorERROR_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_DETECT 플래그와 WINHTTP_AUTOPROXY_CONFIG_URL 플래그를 모두 지정하고 자동 분리 플래그와 자동 구성 URL을 지정하는 경우 WinHttpGetProxyForUrl은 먼저 자동 검색을 시도한 다음 자동 검색이 PAC URL을 찾지 못하면 애플리케이션에서 제공하는 자동 구성 URL로 "폴백"됩니다.

WinHttpDetectAutoProxyConfigUrl 함수

WinHttpDetectAutoProxyConfigUrl 함수는 WPAD 프로토콜의 하위 집합을 구현합니다. PAC 파일을 다운로드하거나 실행하지 않고 프록시 자동 구성 파일의 URL을 자동 검색하려고 시도합니다. 이 함수는 웹 클라이언트 애플리케이션이 PAC 파일 자체의 다운로드 및 실행을 처리해야 하는 특별한 상황에서 유용합니다.

WinHttpGetIEProxyConfigForCurrentUser 함수

WinHttpGetIEProxyConfigForCurrentUser 함수는 "WinInet.dll"를 호출하지 않고 현재 활성 네트워크 연결에 대한 현재 사용자 인터넷 Explorer 프록시 설정을 반환합니다. 이 함수는 대화형 사용자 계정 ID로 실행되는 프로세스 내에서 호출되는 경우에만 유용합니다. 그렇지 않으면 인터넷 Explorer 프록시 구성을 사용할 수 없기 때문입니다. 예를 들어 IIS 서비스 프로세스에서 실행되는 ISAPI DLL에서 이 함수를 호출하는 것은 유용하지 않습니다. WinHTTP 기반 애플리케이션이 WinHttpGetIEProxyConfigForCurrentUser를 사용하는 시나리오에 대한 자세한 내용과 시나리오는 자동 구성 파일 없는 검색을 참조하세요.