解决httpwebrequest无法通过使用代理的局域网获取response的问题

今天在尝试使用Silverlight 调用wcf service的时候总是出现"Unable to connect to the remote server",或者无法找到服务地址之类的异常。

奇怪的是,如果打开fiddler程序就可以正常运行。

于是推断是和代理有关系,因为我们网段使用的是公司的代理,之前看到一篇文章提到网段里面有动态代理可能会导致这种问题,而且fiddler确实会对IE代理进行修改。

最后终于把问题解决了,方法就是给request添加如下设置:

request.Proxy = WebRequest.GetSystemWebProxy();

引用MSDN上的解释就是:

GetSystemWebProxy method reads the current user's Internet Explorer (IE) proxy settings. This process includes the IE options to automatically detect proxy settings, use an automatic configuration script, manual proxy server settings, and advanced manual proxy server settings.

GetSystemWebProxy 方法会读取用户IE的代理设置,包括“自动获取代理设置”、“使用自动化配置脚本”、“手动代理服务器设置”和“高级手动代理设置”。

以上的这些基本覆盖了所有IE代理设置,所以只要IE能正常访问,程序就不会有问题。

原文地址:https://www.cnblogs.com/silverbullet11/p/2538756.html