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

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

问题描述

下面code对我的作品:

The following code works for me:

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

不幸的是, WebProxy.GetDefaultProxy()是pcated德$ P $。还有什么我应该做的?

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,你不应该需要做到这一点。如果没有明确的Proxy属性上的Web请求设置使用静态WebRequest.DefaultWebProxy的价值。如果你想更改代理正在使用的所有后续WebRequests,您可以设置此静态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.

如果你想使用不同的代理服务器设置以当前用户,那么你就需要code

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(新的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.Proxy WebRequest.DefaultWebProxy = NULL 来prevent使用任何代理。这似乎为我工作好,但你可以用任何参数设置为新DefaultProxy()以获得所需的行为。另一件事是检查,如果存在于你的应用程序的配置文件中的代理元素,所以.NET框架将不会使用代理服务器设置在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天全站免登陆