WinHTTP AutoProxy 函数

WinHTTP AutoProxy 函数WinHTTPAutoProxy函数WinHTTPimplementstheWPADprotocolusingtheWinHttpGetProxyForUrlfunctionalongwithtwosupportingutilityfunctions,WinHttpDetectAutoProxyConfigUrlandWinHttpGet

大家好,又见面了,我是你们的朋友全栈君。

WinHTTP AutoProxy 函数

WinHTTP implements the WPAD protocol using the WinHttpGetProxyForUrl function along with two supporting utility functions,WinHttpDetectAutoProxyConfigUrl and WinHttpGetIEProxyConfigForCurrentUser.

WinHTTP依靠WinHttpGetProxyForUrl函数和其余两个辅助函数,WinHttpDetectAutoProxyConfigUrl and WinHttpGetIEProxyConfigForCurrentUser实现了WPAD协议。

AutoProxy support is not fully integrated into the HTTP stack in WinHTTP. Before sending a request, the application must callWinHttpGetProxyForUrl to obtain the name of a proxy server and then callWinHttpSetOption using WINHTTP_OPTION_PROXY to set the proxy configuration on the WinHTTP request handle created byWinHttpOpenRequest.

支持AutoProxy没有完全整合到WinHTTP的HTTP协议栈中。在发送一个请求前,程序必须调用WinHttpGetProxyForUrl来获取代理服务器的名称,然后调用WinHttpSetOption,把参数设为WINHTTP_OPTION_PROXY,这些操作都在WinHTTP创建的请求句柄上完成。

The WinHttpGetProxyForUrl function can execute all three steps of the WPAD protocol described in the previous overview: (1) discover the PAC URL, (2) download the PAC script file, (3) execute the script code and return the proxy configuration in a WINHTTP_PROXY_INFO structure. Optionally, if the application knows in advance the PAC URL it can specify this toWinHttpGetProxyForUrl.

WinHttpGetProxyForUrl函数可以执行上述WPAD协议中的三步操作(1)发现PAC URL(2)下载PAC脚本文件(3)执行脚本代码,然后把代理配置返回到一个WINHTTP_PROXY_INF结构体中。如果程序提前知道PAC URL位置,可以把这个地址作作为参数设置到toWinHttpGetProxyForUrl函数中。

The following example code uses autoproxy. It sets up an HTTP GET request by first creating the WinHTTP session connect and request handles. TheWinHttpOpen call specifies WINHTTP_ACCESS_TYPE_NO_PROXY for the initial proxy configuration, to indicate that requests are sent directly to the target server by default. Using autoproxy, it then sets the proxy configuration directly on the request handle.

下列代码使用了自动代理发现功能,它在先前创建的WinHTTP创建的句柄上发出一个HTTP Get请求。WinHttpOpen函数调用指定WINHTTP_ACCESS_TYPE_NO_PROXY参数作为初始代理设置,这表示请求直接发送给目标服务器。如果使用autoproxy,它直接把代理设置在请求句柄上。

C++

  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) );
  
//
// 创建 WinHTTP open 会话.
//
  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;
  
//
// 创建 WinHTTP connect 句柄.
//
  hConnect = WinHttpConnect( hHttpSession,
                             L"www.microsoft.com",
                             INTERNET_DEFAULT_HTTP_PORT,
                             0 );
  
// Exit if WinHttpConnect failed.
  if( !hConnect )
    goto Exit;
  
//
// 创建 HTTP request 句柄.
//
  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"http://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 );



In the provided example code, the call to WinHttpGetProxyForUrl instructs the function to discover the proxy auto-config file automatically by specifying theWINHTTP_AUTOPROXY_AUTO_DETECT flag in the WINHTTP_AUTOPROXY_OPTIONS structure. Use of the WINHTTP_AUTOPROXY_AUTO_DETECT flag requires the code to specify one or both of the auto-detection flags (WINHTTP_AUTO_DETECT_TYPE_DHCP,WINHTTP_AUTO_DETECT_TYPE_DNS_A). The example code uses the auto-detection feature ofWinHttpGetProxyForUrl because the PAC URL is not known in advance. If a PAC URL cannot be located on the network in this scenario,WinHttpGetProxyForUrl fails (GetLastError returnsERROR_WINHTTP_AUTODETECTION_FAILED).

在上述例程中,设置WINHTTP_AUTOPROXY_OPTIONS结构体中的WINHTTP_AUTOPROXY_AUTO_DETECT标识,然后调用WinHttpGetProxyForUrl函数发现代理自动配置。使用WINHTTP_AUTOPROXY_AUTO_DETECT标识要求代码必须指定一个或两个自动探测标识(WINHTTP_AUTO_DETECT_TYPE_DHCP,WINHTTP_AUTO_DETECT_TYPE_DNS_A)。例程中代码使用WinHttpGetProxyForUrl的自动探测功能,因为实现不知道PAC URL。如果有的时候不能从网络中获取PAC URL,WinHttpGetProxyForUrl就会报错(GetLastError 返回ERROR_WINHTTP_AUTODETECTION_FAILED)。

If the PAC URL is Known in Advance

如果事先知道PAC URL

If the application does know the PAC URL, it can specify it in the WINHTTP_AUTOPROXY_OPTIONS structure and configureWinHttpGetProxyForUrl to skip the auto-detection phase.

如果程序不知道PAC URL,就可以在WINHTTP_AUTOPROXY_OPTIONS结构体中指定好,然后再使用WinHttpGetProxyForUrl,这样就可以跳过自动探测阶段。

For example, if a PAC file is available on the local network at the URL, “http://InternalSite/proxy-config.pac”, the call toWinHttpGetProxyForUrl would look the following.

例如,如果PAC文件存在于网络中以下URL,”http://InternalSite/proxy-config.pac”,对WinHttpGetProxyForUrl的调用参加下面代码。

C++

//
// 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"http://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"http://www.microsoft.com/ms.htm",
                             &AutoProxyOptions,
                             &ProxyInfo ) )
{
  //...



If the WINHTTP_AUTOPROXY_OPTIONS structure specifies both WINHTTP_AUTOPROXY_AUTO_DETECT andWINHTTP_AUTOPROXY_CONFIG_URL flags (and specifies auto-detction flags and an auto-config URL),WinHttpGetProxyForUrl first attempts auto-detection, and then, if auto-detection fails to locate a PAC URL, “falls back” to the auto-config URL supplied by the application.

如果在WINHTTP_AUTOPROXY_OPTIONS结构体中指定了WINHTTP_AUTOPROXY_AUTO_DETECTWINHTTP_AUTOPROXY_CONFIG_URL标志(指定自动探测和自动配置URL),WinHttpGetProxyForUrl首先尝试自动探测,接着,如果自动探测PAC URL失败,会“回跳”到程序设置的自动配置URL部分。

The WinHttpDetectAutoProxyConfigUrl Function

WinHttpDetectAutoProxyConfigUrl函数

The WinHttpDetectAutoProxyConfigUrl function implements a subset of the WPAD protocol: it attempts to auto-detect the URL for the proxy auto-config file, without downloading or executing the PAC file. This function is useful in special situations where a Web client application must handle the download and execution of the PAC file itself.

WinHttpDetectAutoProxyConfigUrl函数实现了WPAD以下子集:尝试自动探测含有自动配置文件的URL,不下载执行PAC文件。这个函数在程序需要自己解析和执行PAC文件时有用。

The WinHttpGetIEProxyConfigForCurrentUser Function

WinHttpGetIEProxyConfigForCurrentUser函数

The WinHttpGetIEProxyConfigForCurrentUser function returns the current user Internet Explorer proxy settings for the current active network connection, without calling into “WinInet.dll”. This function is only useful when called within a process that is running under an interactive user account identity, because no Internet Explorer proxy configuration is likely to be available otherwise. For example, it would not be useful to call this function from an ISAPI DLL running in the IIS service process. For more information and a scenario in which a WinHTTP-based application would useWinHttpGetIEProxyConfigForCurrentUser, see Discovery Without an Auto-Config File.

WinHttpGetIEProxyConfigForCurrentUser函数返回当前用户的活动的IE连接代理设置,而不用调用”WinInet.dll”。这个函数只在有用户交互的环境下有用,因为其它情况好像也不会有IE代理设置。例如在IIS服务下中第ISAPI.dll中调用这个这个函数就没用。如果需要更多信息,下面有个基于WinHTTP的程序使用了WinHttpGetIEProxyConfigForCurrentUser,参见Discovery Without an Auto-Config File.

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/157278.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 线性代数投影矩阵的定义_线性代数a和线性代数b

    线性代数投影矩阵的定义_线性代数a和线性代数bAbout投影矩阵  一个矩阵AAA既可以表示一种线性变换,又可以是一个子空间(由基张开的),还可以是一组坐标,甚是神奇。文章目录About投影矩阵一维空间的投影矩阵投影矩阵的多维推广投影的物理意义信号处理中的正交投影技术一维空间的投影矩阵  查看上图,ppp是bbb在aaa上的投影,可以发现,ppp和aaa是同向的,故可以表示为如下形式,其中xxx是标量p=axp=axp=ax  根据eee和ppp正交的条件,可以推导出x=aTbaTax=\frac{a^Tb}{a^Ta}x=aTaaT

    2022年10月4日
    2
  • linux搭建php运行环境_docker部署php项目

    linux搭建php运行环境_docker部署php项目方案一、phpStudyforLinuxphpStudyforLinux支持Apache/Nginx/Tengine/Lighttpd,支持php5.2/5.3/5.4/5.5切换已经在centos-6.5,debian-7.4.,ubuntu-13.10测试成功使用说明:服务进程管理:phpstudy(start|stop|restart|uninstall)

    2022年9月16日
    2
  • web前端表单制作_注册表单测试用例

    web前端表单制作_注册表单测试用例03- web表单测试

    2022年4月22日
    57
  • leetcode-33搜索旋转排序数组(二分)[通俗易懂]

    leetcode-33搜索旋转排序数组(二分)[通俗易懂]整数数组 nums 按升序排列,数组中的值 互不相同 。在传递给函数之前,nums 在预先未知的某个下标 k(0 <= k < nums.length)上进行了 旋转,使数组变为 [nums[k], nums[k+1], …, nums[n-1], nums[0], nums[1], …, nums[k-1]](下标 从 0 开始 计数)。例如, [0,1,2,4,5,6,7] 在下标 3 处经旋转后可能变为 [4,5,6,7,0,1,2] 。给你 旋转后 的数组 nums 和一个整数 ta

    2022年8月8日
    7
  • influxdb原理与实战_fluent调用nist数据库

    influxdb原理与实战_fluent调用nist数据库本文属于《InfluxDB系列教程》文章系列,该系列共包括以下15部分:InfluxDB学习之InfluxDB的安装和简介InfluxDB学习之InfluxDB的基本概念InfluxDB学习

    2022年8月1日
    17
  • JMeter教程_梦想版图文

    JMeter教程_梦想版图文看了包会

    2025年7月4日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号