Tip: Replacement Methods for Obsolete WebProxy.GetDefaultProxy Method

When we use the .Net HTTP classes to do, say, downloading, we need to set its proxy because some computers, especially those in a large coporate companies, cannot access internet directly, they can only do so though a web proxy server. As we all know the WebProxy.GetDefaultProxy method, to get the IE web proxy setting, is deprecated in .Net 2.0 and greater. Not many people know of its replacement methods. For replacement, you can use WebRequest.GetSystemWebProxy static method or WebRequest.DefaultWebProxy static property. Below is a code example of how to assign WebRequest.GetSystemWebProxy to HttpWebRequest.Proxy.

HttpWebRequest request = WebRequest.Create("Enter some valid url here") as HttpWebRequest;

if(request==null)
	return false;

System.Net.IWebProxy iwpxy = WebRequest.GetSystemWebProxy();
request.Proxy = iwpxy;

HttpWebResponse response = request.GetResponse() as HttpWebResponse;

// do your downloading here

response.Close();

Note: This method gets the IE proxy setting and it only works if the user uses Internet Explorer and therefore sets the proxy setting; if this user use sother web-browser as his/her default web browser, not using IE at all, he/she may not be bothered to set the proxy setting in IE. Here is a tip to get Firefox proxy setting. The steps are defined below.

  • Go to %USERPROFILE%/Application Data/Mozilla/FireFox folder.
  • Open the profile.ini file. (Be careful here because each firefox user can have more than 1 profile)
  • Read the StartWithLastProfile value under "General" section in the profile.ini
  • Find the profile which matches its value in IsRelative value under "ProfileX"(X is a numeric value) section
  • Read the Path value under "ProfileX"(X is a numeric value) section
  • And append this Path value to the previous path, %USERPROFILE%/Application Data/Mozilla/FireFox
  • After appending the folder, go to the folder and open the prefs.js javascript file as a text file.
  • Parse and search for the 2 statements below to detect the proxy IP address and its port. Note: xx is the arbitrary value.
    • user_pref("network.proxy.http", "xx.xx.xx.xx");
    • user_pref("network.proxy.http_port", xxxx);
  • If you cannot find these 2 values, it means proxy is not used because its settings are not set.
  • Use the above 2 values only if you can find this statement "user_pref("network.proxy.type", 1);" which means proxy use is enabled, else proxy is disabled if that statement is not present.
  • Note: Proxy is disabled, if "user_pref("network.proxy.type", 0);" is found

Note: If you get an 407 error of "HTTP Error 407 - Proxy authentication required" message, you need prompt your user to get the his/her user name and password for the proxy to pass them to the Credentials property. And of course you need to store them somewhere encrypted and safe because you do not want to prompt the user for the same credentials, everytime you need to access the internet. Remember to encrypt them and not save them as plain text for security reasons! If the user is using his Windows credential, you can access his credential using System.Net.CredentialCache.DefaultNetworkCredentials. Do not store Windows user name and password on your own because your method, can never be as secure as Microsoft, which may leads to security vulnerabilities which in turn cause customers to shun your product!

History

  • 6th February, 2010 : Cleaned up the code and added DefaultNetworkCredentials information.
  • 16th October, 2009 : First Release


About the Author

Wong Shao Voon

I guess I'll write here what I does in my free time, than to write an accolade of skills which I currently possess. I believe the things I does in my free time, say more about me.

When I am not working, I like to watch Japanese anime. I am also writing some movie script, hoping to see my own movie on the big screen one day.

I like to jog because it makes me feel good, having done something meaningful in the morning before the day starts.

I also writes articles for CodeGuru; I have a few ideas to write about but never get around writing because of hectic schedule.

Comments

  • Perfect!

    Posted by Jonathan Comer on 08/10/2014 04:34am

    Nice bit of code. I've been looking for this for some time. I'd been trying various options but had never got it to work until now.

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • On-demand Event Event Date: October 29, 2014 It's well understood how critical version control is for code. However, its importance to DevOps isn't always recognized. The 2014 DevOps Survey of Practice shows that one of the key predictors of DevOps success is putting all production environment artifacts into version control. In this webcast, Gene Kim discusses these survey findings and shares woeful tales of artifact management gone wrong! Gene also shares examples of how high-performing DevOps …

Most Popular Programming Stories

More for Developers

RSS Feeds