tags:

views:

961

answers:

1

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) I 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 webservice 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 don't really want to go down that route.

A: 

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.

Rob Levine
Just downloaded and tried Fiddler and it did exactly as you had predictedThanks.
sgmoore
I think there's one caveat here: IIRC, .NET picks up the default web proxy at Application startup. If it changes at runtime (say, because you've started Fiddler), I /think/ that the change will not be respected unless you use WebRequest.DefaultWebProxy. But I haven't tried this in over a year.
EricLaw -MSFT-