Search code examples
.net-2.0proxy

Removing obsolete WebProxy.GetDefaultProxy() references


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.


Solution

  • 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

     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();
    

    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.