我应该如何设置默认代理以使用默认凭据? [英] How should I set the default proxy to use default credentials?

查看:28
本文介绍了我应该如何设置默认代理以使用默认凭据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码对我有用:

var webProxy = WebProxy.GetDefaultProxy();
webProxy.UseDefaultCredentials = true;
WebRequest.DefaultWebProxy = webProxy;

不幸的是,WebProxy.GetDefaultProxy() 已弃用.我还应该做什么?

Unfortunately, WebProxy.GetDefaultProxy() is deprecated. What else should I be doing?

(在我的部署中不允许使用 app.config 设置 defaultProxy 设置)

(using app.config to set the defaultProxy settings is not allowed in my deployment)

推荐答案

从 .NET 2.0 开始,您不需要这样做.如果您没有在 Web 请求上显式设置 Proxy 属性,它将使用静态 WebRequest.DefaultWebProxy 的值.如果您想更改所有后续 WebRequest 使用的代理,您可以设置此静态 DefaultWebProxy 属性.

From .NET 2.0 you shouldn't need to do this. If you do not explicitly set the Proxy property on a web request it uses the value of the static WebRequest.DefaultWebProxy. If you wanted to change the proxy being used by all subsequent WebRequests, you can set this static DefaultWebProxy property.

WebRequest.DefaultWebProxy 的默认行为是使用与 Internet Explorer 相同的底层设置.

The default behaviour of WebRequest.DefaultWebProxy is to use the same underlying settings as used by Internet Explorer.

如果您想对当前用户使用不同的代理设置,那么您需要编码

If you wanted to use different proxy settings to the current user then you would need to code

WebRequest webRequest = WebRequest.Create("http://stackoverflow.com/");
webRequest.Proxy = new WebProxy("http://proxyserver:80/",true);

WebRequest.DefaultWebProxy = new WebProxy("http://proxyserver:80/",true);

您还应该记住代理的对象模型包括代理可以根据目标主机名而不同的概念.在调试和检查 webRequest.Proxy 的属性时,这可能会使事情变得有点混乱.打电话

You should also remember the object model for proxies includes the concept that the proxy can be different depending on the destination hostname. This can make things a bit confusing when debugging and checking the property of webRequest.Proxy. Call

webRequest.Proxy.GetProxy(new Uri("http://google.com.au")) 以查看将使用的代理服务器的实际详细信息.

webRequest.Proxy.GetProxy(new Uri("http://google.com.au")) to see the actual details of the proxy server that would be used.

关于是否可以设置 webRequest.ProxyWebRequest.DefaultWebProxy = null 以防止使用任何代理,似乎存在一些争论.这对我来说似乎工作正常,但您可以将它设置为 new DefaultProxy() 而不带参数来获得所需的行为.另一件要检查的事情是,您的应用程序配置中是否存在 proxy 元素文件,.NET Framework 将使用 Internet Explorer 中的代理设置.

There seems to be some debate about whether you can set webRequest.Proxy or WebRequest.DefaultWebProxy = null to prevent the use of any proxy. This seems to work OK for me but you could set it to new DefaultProxy() with no parameters to get the required behaviour. Another thing to check is that if a proxy element exists in your applications config file, the .NET Framework will NOT use the proxy settings in Internet Explorer.

MSDN 杂志文章 通过 .NET 中的自动配置减轻用户的负担 提供了有关幕后发生的事情的更多详细信息.

The MSDN Magazine article Take the Burden Off Users with Automatic Configuration in .NET gives further details of what is happening under the hood.

这篇关于我应该如何设置默认代理以使用默认凭据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆