删除过时的 WebProxy.GetDefaultProxy() 引用
我有一些代码让我很恼火,因为它生成了过时的警告,但我对删除它持谨慎态度,因为:
a)它可以工作
b)我没有编写它
c)我目前没有测试方法它。 (即我无权访问需要的机器)
代码如下
System.Net.WebProxy proxyObject = System.Net.WebProxy.GetDefaultProxy();
proxyObject.Credentials = System.Net.CredentialCache.DefaultCredentials;
proxyObject.BypassProxyOnLocal = true;
System.Net.GlobalProxySelection.Select = proxyObject;
警告消息为
Warning 31 'System.Net.GlobalProxySelection' is obsolete: 'This class has been deprecated. 请改用 WebRequest.DefaultWebProxy 来访问和设置全局默认代理。 使用“null”而不是 GetEmptyWebProxy。 http://go.microsoft.com/fwlink/?linkid=14202'
但是,如果我的理解是正确的,(并且假设程序尝试访问的 Web 服务永远不会是本地的)我真正应该做的就是删除这四行?
这是正确的还是遗漏了什么?
附言。 我知道可能有一个 #pragma 选项可以忽略警告,但我真的不想走这条路。
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) It works
b) I didn't write it
c) I don't currently have a means of testing it. (ie I don't have access to a machine where it is required)
The code is as follows
System.Net.WebProxy proxyObject = System.Net.WebProxy.GetDefaultProxy();
proxyObject.Credentials = System.Net.CredentialCache.DefaultCredentials;
proxyObject.BypassProxyOnLocal = true;
System.Net.GlobalProxySelection.Select = proxyObject;
The warning message is
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'
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. I know there is probably a #pragma option to ignore the warning, but I don't really want to go down that route.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,我认为你是对的。
测试这一点的一个好方法是使用 Fiddler 因为它所做的事情之一(除了跟踪 http requests) 是自动将自己设置为您的 IE 代理。
如果您运行 Fiddler,然后运行以下代码,
您将看到代码中没有显式代理覆盖(如“我真正应该做的就是删除这四行”),该请求确实获取了全局代理来自 IE 的默认代理,您将在 Fiddler 中看到您的请求。
仅当您取消注释空代理分配时,请求才会绕过全局代理设置并且不会出现在 Fiddler 中。
所以是的 - 我认为你是对的; 对于默认代理,您可以删除显式代理代码。
Yes I think you are right.
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.
If you run the Fiddler and then the following piece of code
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.
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.