大家好,又见面了,我是你们的朋友全栈君。
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,它直接把代理设置在请求句柄上。
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的调用参加下面代码。
// // 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.
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/157278.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...