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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • deepfakes视频的网站_deepfakes视频的网站[通俗易懂]

    deepfakes视频的网站_deepfakes视频的网站[通俗易懂]{“optioninfo”:{“dynamic”:”true”,”static”:”true”},”simplifiedDisplay”:”newEdition”,”newCard”:[{“link”:”https://www.aliyun.com/product/live”,”icon”:”live”,”title”:”视频直播LIVE”,”des”:”视频直播(ApsaraVideoLive…

    2022年5月26日
    43
  • MyBatisPlus04_MyBatisPlus的代码自动生成器

    MyBatisPlus04_MyBatisPlus的代码自动生成器MyBatisPlus0 MyBatisPlus 的代码自动生成器添加相关依赖 dependency groupId com baomidou groupId artifactId mybatis plus generator artifactId version 3 3 1 tmp version dependency dependency dependency

    2025年11月23日
    4
  • 计算机如何修改任务管理器,win7如何更改任务管理器快捷键_win7更改任务管理器快捷键的教程…

    计算机如何修改任务管理器,win7如何更改任务管理器快捷键_win7更改任务管理器快捷键的教程…我们在打开任务管理器的时候,通常是CTRL+ALT+DEL就可以快速打开,不过有许多用户装完win7系统之后,发现任务管理器快捷键变成了Ctrl+Shift+Esc,这让用户们用着很不习惯,其实我们也可以自己手动更改快捷键,现在给大家带来win7更改任务管理器快捷键的教程。具体步骤如下:1、在“开始”菜单的搜索框输入指令gpedit.msc,回车打开Win7系统的组策略编辑器。2、在组策略编辑器里…

    2022年6月18日
    34
  • Lamp架构_10个人公司的架构图

    Lamp架构_10个人公司的架构图一:LAMP架构简介LAMP是目前成熟的一种企业网站应用模式之一,指的是协同工作的一套系统和相关软件的整合,可提供PHP动态web站点应用及开发环境,LAMP经过十年的完善各个组件间的兼容性,协作能力,稳定等方面也不断增强(注:Apache服务是一个静态网站,它里面的测试页都是以HTML的格式结尾,以HTML写出的网站都是静态的,没有什么功能,没有办法去关联后台的数据库,所以说他只能做一个展示的页面,LAMP构建出来就是一个动态网页的一个后台,PHP是一种动态网站开发语言,…

    2022年10月16日
    3
  • Matlab粒子群算法(PSO)优化程序——经典实例

    Matlab粒子群算法(PSO)优化程序——经典实例粒子群算法(ParticleSwarmOptimization,PSO)最早是由Eberhart和Kennedy于1995年提出,它的基本概念源于对鸟群觅食行为的研究。鸟群中有个体和群体,个体和群体的信息是可以互通的。个体在随机搜寻食物的过程中,只要跟踪离食物最近的群体,就能最有效地找到食物。1.一些基本概念:(1)粒子:优化问题的候选解,指鸟群中的一个个个体;(2)位置:候选解所在…

    2022年5月28日
    38
  • C语言实现五子棋小游戏

    C语言实现五子棋小游戏三子棋,五子棋,无论多少子棋,其原理都是一样的。下面我用五子棋为例讲解用C语言多文件编程实现五子棋。设计电脑和玩家两个作为下棋的两方,用键盘输入作为玩家的游戏操作。1.效果图:程序总的构架:我们只要输入坐标就可以和电脑对弈了。电脑的棋子用‘0’表示,玩家的棋子用‘x’表示。2.打印菜单可以根据自己的爱好设计各种风格的…

    2022年5月12日
    44

发表回复

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

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