码迷,mamicode.com
首页 > 其他好文 > 详细

侦测IE的代理服务器

时间:2015-09-03 10:21:20      阅读:2781      评论:0      收藏:0      [点我收藏+]

标签:

http://blog.csdn.net/cnjet/article/details/8284127

 

所有的网络程序使用HTTP服务时,必然会遇到可能需要检测代理服务器的情况,程序可以提供界面供玩家设置,但是更常用的一种方式是自动检测IE浏览器的代理设置,并以此为代理来设置应用程序的代理信息,Opera, Firefox, Chrome等主流浏览器都是如此。以下程序使用WinHTTP的API实现自动的代理检测,支持“自动检测”,“自动检测代理文件”,“手动设置”,无代理等各种情况。

 

 

[cpp] view plaincopy
 
  1. int GetHTTPProxyFromIE(const WCHAR* url, /*out*/std::wstring &server )  
  2. {  
  3.     BOOL                      auto_proxy = FALSE;  
  4.     WINHTTP_AUTOPROXY_OPTIONS auto_proxy_option;  
  5.     WINHTTP_PROXY_INFO        auto_proxy_info;  
  6.   
  7.     ZeroMemory( &auto_proxy_option, sizeof(WINHTTP_AUTOPROXY_OPTIONS) );  
  8.     ZeroMemory( &auto_proxy_info, sizeof(WINHTTP_PROXY_INFO) );  
  9.   
  10.     //  
  11.     // convert URL from mbs to wchar  
  12.     //  
  13.     const WCHAR* url_request = L"http://www.youtube.com";  
  14.     if( NULL!=url && wcslen(url)>0 )  
  15.         url_request = url;  
  16.   
  17.     server = L"";  
  18.   
  19.     //  
  20.     // Create the WinHTTP session.  
  21.     //  
  22.     HINTERNET http_session = WinHttpOpen( L"ProxyDetect/1.0",  
  23.         WINHTTP_ACCESS_TYPE_NO_PROXY,  
  24.         WINHTTP_NO_PROXY_NAME,  
  25.         WINHTTP_NO_PROXY_BYPASS,  
  26.         0 );  
  27.   
  28.     if( NULL==http_session ) {  
  29.         return -2;  
  30.     }  
  31.   
  32.     if (!WinHttpSetTimeouts( http_session, 30000, 30000, 0, 0)) {  
  33.         printf( "Error %u in WinHttpSetTimeouts.\n", GetLastError());  
  34.   
  35.         WinHttpCloseHandle(http_session);  
  36.         return -3;  
  37.     }  
  38.   
  39.     //  
  40.     // Check if need auto proxy.  
  41.     //  
  42.     WINHTTP_CURRENT_USER_IE_PROXY_CONFIG ie_config;   
  43.     if( WinHttpGetIEProxyConfigForCurrentUser(&ie_config) ) {  
  44.         if( ie_config.fAutoDetect )  
  45.             auto_proxy = TRUE;  
  46.   
  47.         if( NULL!=ie_config.lpszAutoConfigUrl ) {  
  48.             auto_proxy = TRUE;  
  49.             auto_proxy_option.lpszAutoConfigUrl = ie_config.lpszAutoConfigUrl;  
  50.         }  
  51.     } else {  
  52.         // Error, Try to auto proxy.  
  53.         auto_proxy = TRUE;  
  54.     }  
  55.   
  56.     //  
  57.     // request auto detect to get proxy info.  
  58.     //  
  59.     if( auto_proxy ) {  
  60.         // Setting auto detect options  
  61.         if( NULL!=auto_proxy_option.lpszAutoConfigUrl ) {  
  62.             auto_proxy_option.dwFlags = WINHTTP_AUTOPROXY_CONFIG_URL;  
  63.         } else {  
  64.             auto_proxy_option.dwFlags = WINHTTP_AUTOPROXY_AUTO_DETECT;  
  65.             auto_proxy_option.dwAutoDetectFlags = WINHTTP_AUTO_DETECT_TYPE_DHCP|WINHTTP_AUTO_DETECT_TYPE_DNS_A;  
  66.         }  
  67.   
  68.         // basic flags you almost always want  
  69.         auto_proxy_option.fAutoLogonIfChallenged = TRUE;  
  70.   
  71.         // here we reset fAutoProxy in case an auto-proxy isn‘t actually  
  72.         // configured for this URL  
  73.         auto_proxy = WinHttpGetProxyForUrl(http_session, url_request, &auto_proxy_option, &auto_proxy_info );  
  74.     }  
  75.   
  76.     //  
  77.     // Output result  
  78.     //  
  79.     if( auto_proxy ) {  
  80.         // set proxy options based on auto proxy info.  
  81.         server = auto_proxy_info.lpszProxy;  
  82.     } else  if( NULL!=ie_config.lpszProxy ) {  
  83.         // IE has an explicit proxy. set proxy options for libcurl here  
  84.         // based on ieProxyConfig  
  85.         //  
  86.         // note that sometimes IE gives just a single or double colon  
  87.         // for proxy or bypass list, which means "no proxy"  
  88.         server = ie_config.lpszProxy;  
  89.     } else {  
  90.         // there is no auto proxy and no manually configured proxy  
  91.     }  
  92.   
  93.     //  
  94.     // Clean up.  
  95.     //  
  96.     if( ie_config.lpszAutoConfigUrl != NULL )  
  97.         GlobalFree(ie_config.lpszAutoConfigUrl);  
  98.   
  99.     if( auto_proxy_info.lpszProxy != NULL )  
  100.         GlobalFree(auto_proxy_info.lpszProxy);  
  101.   
  102.     if( auto_proxy_info.lpszProxyBypass != NULL )  
  103.         GlobalFree( auto_proxy_info.lpszProxyBypass );  
  104.   
  105.     if( http_session != NULL )  
  106.         WinHttpCloseHandle( http_session );  
  107.   
  108.     return 0;  
  109. }  



源代码

----------------------------------------

int GetHTTPProxyFromIE(const WCHAR* url, /*out*/std::wstring &server )
{
BOOL auto_proxy = FALSE;
WINHTTP_AUTOPROXY_OPTIONS auto_proxy_option;
WINHTTP_PROXY_INFO auto_proxy_info;

ZeroMemory( &auto_proxy_option, sizeof(WINHTTP_AUTOPROXY_OPTIONS) );
ZeroMemory( &auto_proxy_info, sizeof(WINHTTP_PROXY_INFO) );

//
// convert URL from mbs to wchar
//
const WCHAR* url_request = L"http://www.youtube.com";
if( NULL!=url && wcslen(url)>0 )
url_request = url;

server = L"";

//
// Create the WinHTTP session.
//
HINTERNET http_session = WinHttpOpen( L"ProxyDetect/1.0",
WINHTTP_ACCESS_TYPE_NO_PROXY,
WINHTTP_NO_PROXY_NAME,
WINHTTP_NO_PROXY_BYPASS,
0 );

if( NULL==http_session ) {
return -2;
}

if (!WinHttpSetTimeouts( http_session, 30000, 30000, 0, 0)) {
printf( "Error %u in WinHttpSetTimeouts.\n", GetLastError());

WinHttpCloseHandle(http_session);
return -3;
}

//
// Check if need auto proxy.
//
WINHTTP_CURRENT_USER_IE_PROXY_CONFIG ie_config;
if( WinHttpGetIEProxyConfigForCurrentUser(&ie_config) ) {
if( ie_config.fAutoDetect )
auto_proxy = TRUE;

if( NULL!=ie_config.lpszAutoConfigUrl ) {
auto_proxy = TRUE;
auto_proxy_option.lpszAutoConfigUrl = ie_config.lpszAutoConfigUrl;
}
} else {
// Error, Try to auto proxy.
auto_proxy = TRUE;
}

//
// request auto detect to get proxy info.
//
if( auto_proxy ) {
// Setting auto detect options
if( NULL!=auto_proxy_option.lpszAutoConfigUrl ) {
auto_proxy_option.dwFlags = WINHTTP_AUTOPROXY_CONFIG_URL;
} else {
auto_proxy_option.dwFlags = WINHTTP_AUTOPROXY_AUTO_DETECT;
auto_proxy_option.dwAutoDetectFlags = WINHTTP_AUTO_DETECT_TYPE_DHCP|WINHTTP_AUTO_DETECT_TYPE_DNS_A;
}

// basic flags you almost always want
auto_proxy_option.fAutoLogonIfChallenged = TRUE;

// here we reset fAutoProxy in case an auto-proxy isn‘t actually
// configured for this URL
auto_proxy = WinHttpGetProxyForUrl(http_session, url_request, &auto_proxy_option, &auto_proxy_info );
}

//
// Output result
//
if( auto_proxy ) {
// set proxy options based on auto proxy info.
server = auto_proxy_info.lpszProxy;
} else if( NULL!=ie_config.lpszProxy ) {
// IE has an explicit proxy. set proxy options for libcurl here
// based on ieProxyConfig
//
// note that sometimes IE gives just a single or double colon
// for proxy or bypass list, which means "no proxy"
server = ie_config.lpszProxy;
} else {
// there is no auto proxy and no manually configured proxy
}

//
// Clean up.
//
if( ie_config.lpszAutoConfigUrl != NULL )
GlobalFree(ie_config.lpszAutoConfigUrl);

if( auto_proxy_info.lpszProxy != NULL )
GlobalFree(auto_proxy_info.lpszProxy);

if( auto_proxy_info.lpszProxyBypass != NULL )
GlobalFree( auto_proxy_info.lpszProxyBypass );

if( http_session != NULL )
WinHttpCloseHandle( http_session );

return 0;
}

 

 

 

参考信息:

http://curl.haxx.se/mail/lib-2001-08/att-0170/winproxy.c

http://stackoverflow.com/questions/202547/how-do-i-find-out-the-browsers-proxy-settings

侦测IE的代理服务器

标签:

原文地址:http://www.cnblogs.com/gamekk/p/4779933.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!