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.

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

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

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • 21计算机保研经验分享

    21计算机保研经验分享保研最终去向:哈工大威海-计算机个人情况:学校是211计算机弱校,rank7%;个人有数学建模,小程序,网安的省级奖,几个小科研项目,一段工作室经历,擅长后端搬砖。无论文;自我感觉算是保研er水平一般的,我这个去向怎么样啊,欢迎留言面经:吉林大学软件工程+哈工大威海计算机面试经验分享马上写好一、夏令营夏令营经历:北理工网安入营+时间冲突放弃,只能说非常可惜;吉林大学软件入营+优秀营员;哈工大威海计算机入营+面试合格(共投递11所学校学院,只有两个真正参加,但万幸都有收获)吉林大学软件工程

  • iOS插件列表

    iOS插件列表

  • Java数组转list,原来是这样–有陷阱![通俗易懂]

    Java数组转list,原来是这样–有陷阱![通俗易懂]最近开发中,业务上处理,经常用到asList方法,这让我不经想起了它的很多容易让人犯错的地方或者误解的地方,所以就想抽出时间来,整理一下,和大家分享出来,深夜了,话不多说,主要以代码为主,简易的代码,你一看就知道了!大家都知道这个方法是将数组转成list,是JDK中java.util包中Arrays类的静态方法。大家使用时一定要注意(请看代码和注释,一看就明了了): Strings[]…

  • 三、java编译器[通俗易懂]

    三、java编译器[通俗易懂]java编译器、解析器(bytecodeintepreter)、JIT(justintimecompiler)

  • matlab数学建模人口预测模型_三层bp神经网络模型图

    matlab数学建模人口预测模型_三层bp神经网络模型图对中国1949年-2019年的人口特征数据进行分析,基于BP神经网络模型在matlab上实现,对中国每年增长人口数量进行预测。

  • SQL Server 2016 JSON原生支持实例说明

    SQL Server 2016 JSON原生支持实例说明

    2021年11月28日

发表回复

您的电子邮箱地址不会被公开。

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