2015-10-07 47 views
2

我正在开发需要与某些Web服务进行通信的Windows WPF应用程序。我现在需要为坐在代理之后的特定用户实现支持。在一个特定的情况下,用户自动与WPAD通过Internet Explorer设置,获取代理服务器设置:通过在WPAD中定义的代理与Web服务进行通信

enter image description here

我经历过许多文章犁关于代理,但我根本无法得到它的工作。以下是我已经试过:

WebRequest request = WebRequest.Create("http://www.google.com"); 
request.Proxy = WebRequest.GetSystemWebProxy(); 
var response = (HttpWebResponse)request.GetResponse(); 

为WebRequest.GetSystemWebProxy()文档说:

“返回与 当前模拟用户的Internet Explorer设置配置了代理。”

它似乎从Internet Explorer中获取设置。当我调试和检查代理我看到它检测到WPAD文件:

enter image description here

但它似乎没有反正工作。当的GetResponse()被调用时失败,出现以下异常消息:

“连接尝试失败,因为连接的方没有 一段时间后正确响应或已建立的连接 失败,因为连接的主机没有回应“

据我所知,我不应该需要在我的app.config文件中有任何东西。那么我错过了什么?

回答

0

您需要设置代理元素在你的app.config文件:

<system.net> 
    <defaultProxy enabled="true" useDefaultCredentials="true" > 
     <proxy autoDetect="true" scriptLocation="http:/wpad.dat"/> 
    </defaultProxy> 
</system.net> 

https://msdn.microsoft.com/en-us/library/sa91de1e(v=vs.110).aspx

做的另一种方式,但它被认为是遗产是:

WebProxy proxyObject = new WebProxy("http://proxyserver:80/",true); 
WebRequest request = WebRequest.Create("http://www.google.com"); 
request.Proxy = proxyObject; 
+0

是那真的有必要吗?因为它看起来像GetSystemWebProxy()发现了wpad.data文件? – sundown

相关问题