以下代码适用于我:
var webProxy = WebProxy.GetDefaultProxy();
webProxy.UseDefaultCredentials = true;
WebRequest.DefaultWebProxy = webProxy;
不幸的是,WebProxy.GetDefaultProxy()
已被弃用。我还应该做些什么呢?
(我的部署中不允许使用app.config设置defaultProxy设置)
发布于 2012-07-11 17:44:44
您可以使用反射将代码中的UseDefaultCredentials
-Property设置为"true“
System.Reflection.PropertyInfo pInfo = System.Net.WebRequest.DefaultWebProxy.GetType().GetProperty("WebProxy",
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
((System.Net.WebProxy)pInfo.GetValue(System.Net.WebRequest.DefaultWebProxy, null)).UseDefaultCredentials = true;
发布于 2017-01-25 17:44:07
在一些较新的应用程序中,配置似乎有所不同,就像我在这个问题How to authenticate against a proxy when using the HttpClient class?上看到的那样
<system.net>
<defaultProxy enabled="true" useDefaultCredentials="true">
<proxy usesystemdefault="True" />
</defaultProxy>
</system.net>
在https://msdn.microsoft.com/en-us/library/dkwyc043.aspx上也有记录
发布于 2013-12-11 18:24:14
这个帖子很老了,但我最近才偶然发现了defaultProxy的问题,也许它对其他人有帮助。
我按照Andrew的建议使用了config设置。在部署它时,我的客户收到一个错误,说没有足够的权限来设置配置'defaultProxy‘。
我不知道为什么我没有权限设置这个配置,也不知道如何处理它,我只是删除了它,它仍然有效。因此,在VS2013中,这个问题似乎得到了解决。
当我们这样做的时候:
WebRequest.DefaultWebProxy.Credentials = new NetworkCredential("ProxyUsername", "ProxyPassword");
使用默认代理和您的凭据。如果您想强制不使用代理,只需将DefaultWebProxy设置为null (尽管我不知道是否希望如此)。
https://stackoverflow.com/questions/299940
复制相似问题