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


相关推荐

  • 从零开始搭建服务器,拥有一个属于自己的网站

    从零开始搭建服务器,拥有一个属于自己的网站创建一个属于自己,任何人都可以访问的网站(最最最详细的步骤)这篇文章将从购买服务器一直到最后网站完成备案,详细说明整个过程,就算是不懂编程的人照样可以拥有属于自己的服务器和网站必备条件:1:电脑一台2:网络3:money(30块钱)4:身份证一:首先选择阿里云、腾讯云或者其他任何一家的云,租一台ECS服务器。不管是哪一家的云服务器,基本上都有十块钱一个月的服务器,阿里云12-24岁自动认证学生,可以享受十块钱一个月的价格,这里就以阿里云为例。(注意:选择哪一家的服务器最好就使用哪一家的域名注

    2022年5月5日
  • JAVA设计模式之原型模式

    定义:用原型实例指定创建对象的种类,并通过拷贝这些原型创建新的对象。类型:创建类模式类图:原型模式主要用于对象的复制,它的核心是就是类图中的原型类Prototype。Prototype类需要具备以下两个条件:实现Cloneable接口。在java语言有一个Cloneable接口,它的作用只有一个,就是在运行时通知虚拟机可以安全地在实现了此接口的类上使用clone方法。在ja

    2022年3月11日
  • 英雄联盟 源代码_c语言游戏源码

    英雄联盟 源代码_c语言游戏源码【C语言】英雄联盟英雄查询系统模拟模拟~~~程序介绍:【C语言】英雄联盟英雄查询系统/*———————————-project:英雄联盟英雄查询系统author:LLZtoolman:xyfinish:2019/12/26———————————-*//*—————————–⊙注意事项!!!!为了用C语言实现文件的读写程序读取数据

    2022年9月19日
  • 华为悦盒ec6108v9a怎么刷机_华为悦盒官方固件

    华为悦盒ec6108v9a怎么刷机_华为悦盒官方固件华为悦盒EC6108V9A一、产品型号二、工具准备与资料下载1.[当贝网站教程及工具下载](https://www.znds.com/jc/article/7020-1.html)(此网站的工具可以用,但当贝的教程极其混乱,仅供动手者参考)2.固件路径三、刷机教程1.进入盒子的Androidsystemrecovery<3e>2.清除数据3.选择Applyupdatefromexternalstorage四、盒子默认密码五、常用应用一、产品型号名称型号华为悦

    2022年10月27日
  • Python那么火,到底能用来做什么?我们来说说Python3的主要应用

    Python那么火,到底能用来做什么?我们来说说Python3的主要应用

    2021年6月14日
  • java中高级面试题总结(全面)_java面试题大全

    java中高级面试题总结(全面)_java面试题大全jvm结构原理,GC工作原理Jvm结构:Jvm主要包括四个部分:1、类加载器(ClassLoad)在JVM启动时或者在类运行时将需要的class加载到JVM中。类加载时间与过程:类从被加载到虚拟机内存开始,在到卸载出内存为止,正式生命周期包括了:加载,验证,准备,解析,初始化,使用和卸载7个阶段。其中验证、准备、解析这个三个步…

    2022年8月20日

发表回复

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

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