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: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • A modern mobile IT strategy is no longer an option, it is an absolute business necessity. Today's most productive employees are not tied to a desk, an office, or a location. They are mobile. And your company's IT strategy has to be ready to support them with easy, reliable, 24/7 access to the business information they need, from anywhere in the world, across a broad range of communication devices. Here's how some of the nation's most progressive corporations are meeting the many needs of their mobile workers …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds