在WinHTTP中设置WinINet 代理配置

在WinHTTP中设置WinINet 代理配置

Setting Automatic Proxy on WinHTTP 5.1

在WinHTTP 5.1中设置Automatic Proxy

Applications that port from WinINet to WinHTTP may need to use the same autoproxy settings that they can retrieve under WinINet or Internet Explorer (IE). The WinHTTP version 5.1 API can retrieve and use these proxy settings. In general, WinHTTP specifies the proxy and proxy bypass servers on a per-session basis when the session is created. These settings can be overridden on a per-request basis.

从WinINet 向 WinHTTP迁移的程序可能需要使用相同的autoproxy设置,它们可以在WinINet下获取或从IE设置里面获取。WinHTTP 5.1 API可以获取和使用这些代理设置.一般情况下,WinHTTP在会话创建后就要为每次请求指定是使用代理或不使用代理。这些设置可以在每次访问设置的基础上重设。

To use the same proxy configuration as WinINet or IE, the WinHTTP client should set proxy settings for the session. In addition, if IE or WinINet are configured to use Web Proxy Auto-Discovery (WPAD), the WinHTTP client that uses those settings must set proxy settings on a per-request basis. The following sections describe how to specify the proxy settings for a session and a request:

要使用和WinINet 或IE相同的代理设置,WinHTTP客户应该为会话设置代理设置。此外如果IE 或WinINet设置为使用WPAD,那么WinHTTP客户端如果要使用它们需要每次都要做设定。下列章节描述了如何为会话和请求指定代理。

Setting the Proxy Configuration on a Session

为会话指定代理

The Application is Running on a User Account

程序运行于用户帐户下

Before a session is created, the application calls WinHttpGetIEProxyConfigForCurrentUser to get the IE proxy settings. The application must be running as a user account to obtain these settings. ThepProxyConfig parameter is a pointer to a WINHTTP_CURRENT_USER_IE_PROXY_CONFIG structure that contains the proxy name (lpszProxy) and proxy bypass (lpszProxyBypass) servers. The proxy name and proxy bypass values of theWINHTTP_CURRENT_USER_IE_PROXY_CONFIG structure are then used to initialize the WinHTTP session. The session is initialized by callingWinHttpOpen with the pwszProxyName and pwszProxyBypass parameters obtained from thelpszProxy and lpszProxyBypass members of the WINHTTP_CURRENT_USER_IE_PROXY_CONFIG structure.

在会话被创建前,程序调用WinHttpGetIEProxyConfigForCurrentUser获取IE代理设置。程序必须以用户帐户状体来获取这些设置。pProxyConfig参数是一个指向WINHTTP_CURRENT_USER_IE_PROXY_CONFIG结构体的指针,这个结构体包含代理名称(lpszProxy) 和不使用代理的目标 (lpszProxyBypass) 。然后使用设好参数的WINHTTP_CURRENT_USER_IE_PROXY_CONFIG结构体初始化WinHTTP会话。调用WinHttpOpen初始化会话,pwszProxyName 和pwszProxyBypass 参数分别从WINHTTP_CURRENT_USER_IE_PROXY_CONFIG结构体中的lpszProxy 和lpszProxyBypass中获取。

The Application is Running as a Service

程序以服务形式运行

The application must ensure that the registry settings for an individual user are loaded into the registry before callingWinHttpGetIEProxyConfigForCurrentUser. If these settings are not loaded into the registry,WinHttpGetIEProxyConfigForCurrentUser cannot obtain the proxy settings. Registry settings for an individual user can be loaded into the registry by calling theLoadUserProfile function. If loading the user's registry settings is not an option, the application can callWinHttpOpen with the WINHTTP_ACCESS_TYPE_DEFAULT_PROXY specified in thedwAcessType parameter. Specifying the default proxy in the call to WinHttpOpen tells the WinHTTP API to retrieve the proxy configuration set by using the WinHTTPproxycfg.exe utility. After the registry settings for an individual user have been loaded, the application follows the steps outlined underThe application is running on a user account to set the proxy name and proxy bypass servers.

程序必须保证在调用WinHttpGetIEProxyConfigForCurrentUser前把用户的注册表设置被加载到注册表。如果这些设置没有被加载到注册表,那么WinHttpGetIEProxyConfigForCurrentUser不能获取代理设置。加载用户注册表可以通过调用LoadUserProfile实现。如果调用用户注册表不能用,程序可以在调用WinHttpOpen函数时使用WINHTTP_ACCESS_TYPE_DEFAULT_PROXY指定dwAcessType参数。这样设置是告诉WinHTTP API使用proxycfg.exe获取代理配置。在用户注册表被加载后,程序使用The application is running on a user account章节中的步骤来获取代理名称和不使用代理服务器的目标。

Setting the Proxy Configuration on a Single Request

在一次代理请求中配置代理服务器配置

Before the session is created, the application calls WinHttpGetIEProxyConfigForCurrentUser to determine if WinINet and IE are configured to use WPAD.WinHttpGetIEProxyConfigForCurrentUser returns the WINHTTP_CURRENT_USER_IE_PROXY_CONFIG structure that contains thefAutoDetect member. A value of TRUE for this member indicates that WPAD is used, and thelpszAutoConfigUrl member contains the WPAD URL.

在会话被创建前,程序调用WinHttpGetIEProxyConfigForCurrentUser函数确定WinINet 和IE是否配置为WPAD。WinHttpGetIEProxyConfigForCurrentUser返回WINHTTP_CURRENT_USER_IE_PROXY_CONFIG结构体,结构体中包含fAutoDetect成员,这个成员设为TURE时表示WPAD可以使用,lpszAutoConfigUrl包含了WPAD URL。

Automatic Proxy Configuration Is Used

应用了Automatic Proxy 配置

If WPAD is used, the application calls WinHttpGetProxyForUrl to retrieve the proxy for the request. The lpwszUrl parameter contains the URL that the request is being sent to, and the pAutoProxyOptions parameter contains a pointer to the structure ( WINHTTP_AUTOPROXY_OPTIONS) that contains the autoproxy options. The application initializes the WINHTTP_AUTOPROXY_OPTIONS structure with the settings returned from the WINHTTP_CURRENT_USER_IE_PROXY_CONFIG structure in the call to WinHttpGetIEProxyConfigForCurrentUser. The WINHTTP_AUTOPROXY_CONFIG_URL flag is specified in the dwFlags member of the WINHTTP_AUTOPROXY_OPTIONS structure, and the lpszAutoconfigUrl member contains the proxy auto-configuration URL from the WINHTTP_CURRENT_USER_IE_PROXY_CONFIG structure. The WinHttpGetProxyForUrl function returns the proxy name and the proxy bypass list in the lpszProxy and lpszProxyBypass members of the WINHTTP_PROXY_INFO structure.

如果使用了WPAD,程序调用WinHttpGetProxyForUrl来为请求获取代理。lpwszUrl参数包含URL目标,pAutoProxyOptions参数指向一个结构体(WINHTTP_AUTOPROXY_OPTIONS,这个结构体中包含自动代理设置)。程序使用WinHttpGetIEProxyConfigForCurrentUser函数返回的WINHTTP_CURRENT_USER_IE_PROXY_CONFIG结构体初始化WINHTTP_AUTOPROXY_OPTIONS结构体。WinHttpGetProxyForUrl函数返回WINHTTP_PROXY_INFO结构体,结构体在lpszProxy 和lpszProxyBypass成员中包含代理名称和绕过代理设置。

After the proxy for the request is obtained from WinHttpGetProxyForUrl, the application creates the request withWinHttpOpenRequest. Then WinHttpSetOption is called to set the proxy for the request by specifying the request handle in thehInternet parameter. The dwOption parameter in the call to WinHttpSetOption should be set to WINHTTP_OPTION_PROXY and thelpBuffer parameter is a pointer to a WINHTTP_PROXY_INFO structure that contains the proxy and proxy bypass to be used for the request.

在使用WinHttpGetProxyForUrl获取请求代理后,程序使用WinHttpOpenRequest创建请求。然后调用WinHttpSetOption为请求设置代理,方法是在hInternet参数中设定请求句柄。WinHttpSetOption中的dwOption参数可以设定为WINHTTP_OPTION_PROXYlpBuffer参数是一个指向WINHTTP_PROXY_INFO结构体的指针,这个机构体中有代理服务器和不使用代理服务器的设置。


Automatic Proxy Configuration Is Not Used

没有使用Automatic Proxy配置

If the call to WinHttpGetIEProxyConfigForCurrentUser indicates that autoproxy is not used, the application can simply create the request withWinHttpOpenRequest. The proxy configuration is the same for the entire session, and per-request changes are not needed.

如果调用WinHttpGetIEProxyConfigForCurrentUser后发现没用autoproxy,程序就可以简单调用WinHttpOpenRequest创建请求。代理配置就和整个会话一致,每次都去设置这种情况就不需要了。

猜你喜欢

转载自blog.csdn.net/edger2heaven/article/details/45715895