删除过时的WebProxy.GetDefaultProxy()引用 [英] Removing obsolete WebProxy.GetDefaultProxy() references

查看:254
本文介绍了删除过时的WebProxy.GetDefaultProxy()引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些代码使我很烦,因为它会生成过时的警告,但是我很警惕将其删除,因为:

I have a bit of code which is annoying me because it is generating obsolete warnings, but I am wary of removing it because:

a)有效

b)我没有写它

c)我目前没有测试它的方法。 (即我无法访问需要它的机器)

c) I don't currently have a means of testing it. (ie I don't have access to a machine where it is required)

代码如下

System.Net.WebProxy proxyObject = System.Net.WebProxy.GetDefaultProxy();
proxyObject.Credentials = System.Net.CredentialCache.DefaultCredentials;
proxyObject.BypassProxyOnLocal = true;
System.Net.GlobalProxySelection.Select = proxyObject;

警告消息是

警告31 'System.Net.GlobalProxySelection'已作废:'该类已被弃用。请改用WebRequest.DefaultWebProxy来访问和设置全局默认代理。使用 null代替GetEmptyWebProxy。 http://go.microsoft.com/fwlink/?linkid=14202 '

Warning 31 'System.Net.GlobalProxySelection' is obsolete: 'This class has been deprecated. Please use WebRequest.DefaultWebProxy instead to access and set the global default proxy. Use 'null' instead of GetEmptyWebProxy. http://go.microsoft.com/fwlink/?linkid=14202'

但是,如果我的理解是正确的,并且假设程序正在尝试访问的Web服务永远不会是本地的,那么我真正应该做的就是删除这些四行?

But, if my understanding is correct, (and assuming that the web service the program is trying to access will never be local) what I really should do is just delete these four lines?

这是正确的,还是错过了一些东西?

Is this correct, or have a missed something?

PS。我知道可能有一个#pragma选项可以忽略该警告,但是我真的不想沿着那条路线走。

PS. I know there is probably a #pragma option to ignore the warning, but I don't really want to go down that route.

推荐答案

是的,我认为您是对的。

一个很好的测试方法是使用 Fiddler 因为它所做的一件事(除了跟踪http请求)是自动将其自身设置为您的IE代理。

A good way to test this is with Fiddler because one of the things it does (apart from trace http requests) is to automatically set itself up as your IE proxy.

如果您运行Fiddler,然后运行以下代码

If you run the Fiddler and then the following piece of code

 HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://news.bbc.co.uk");
 //request.Proxy = null; // uncomment this to bypass the default (IE) proxy settings
 HttpWebResponse response = (HttpWebResponse)request.GetResponse();

 Console.WriteLine("Done - press return");
 Console.ReadLine();

您将看到代码中没有显式代理覆盖(如我真正应该做的只是删除这四行),请求的确从IE中提取了全局默认代理,您在Fiddler中看到您的请求。

you will see that with no explicit proxy override in the code (as in "what I really should do is just delete these four lines"), the request does indeed pick up the global default proxy from IE, and you will see your request in Fiddler.

仅当您取消注释空代理分配时,请求才会绕过全局代理设置并且不会出现在Fiddler中。

Only when you uncomment the null proxy assignment, does the request bypass the global proxy settings and not appear in Fiddler.

所以是的-我认为您是对的;对于默认代理,您可以删除显式代理代码。

So yes - I think you are right; for the default proxy you can remove the explicit proxy code.

这篇关于删除过时的WebProxy.GetDefaultProxy()引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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