Get past that Proxy server - Method 1 (using code)
by Hans on 2012/05/16 07:55
in Categories: C#

Company firewalls and proxy servers are common in the work place. So the question is when creating an application that will reside inside the company network that needs to access web resources outside the company network just how do we get past the proxy server. This is post will demonstrate the first of two methods. This method will show in code how to get past the proxy server the second post will show how to use configuration to achieve the same thing. The second method being extremely helpful especially when you don't have access or the ability to change the underlying code as per the first method.


Method 1: Code your way past the proxy server

        /// <summary>
        /// This method executes a web request and returns a response
        /// </summary>
        /// <param name="uri">The URL of the resource you are trying toaccess</param>
        /// <param name="useProxy">A boolean indicating whether or not toprovide proxy settings</param>
        /// <param name="proxyUri">The URL of the proxy server, if needed</param>
        /// <param name="userName">The username to use when accessing the proxy server</param>
        /// <param name="password">The password to use when accessing the proxy server</param>
        /// <param name="domain">The domain to use when accessing the proxy server</param>
        /// <returns>a string reply</returns>
        private static string GetResponse( string uri, bool useProxy, string proxyUri, string userName, string password, string domain )
            System.Net.HttpWebRequest request = null;
            System.Net.HttpWebResponse response = null;

            request = ( System.Net.HttpWebRequest ) System.Net.WebRequest.Create( uri );

            request.Method = "GET"; //in this case we will do a simple get
            request.KeepAlive = false;
            request.Timeout = 60000; //some sort of timeout value

            //if we need access and authenticate against a proxy serverthen we need the next 4 lines of code
            if( useProxy )
                //this creates a concrete instance of a proxy class that implements System.Net.IWebProxy
                System.Net.WebProxy proxy = new System.Net.WebProxy();
                proxy.Address = new Uri( proxyUri );

                // Note the following method has 3 different overloads, usethe correct one for your requirements i.e. with or without domain name
                // dont think you can supply the domain name as part of theusername like this "mydomain\myusername" IT WILL NOT WORK

                proxy.Credentials = new System.Net.NetworkCredential( userName, password, domain );
                request.Proxy = proxy;

            response = ( System.Net.HttpWebResponse ) request.GetResponse();
            System.IO.StreamReader reader = new System.IO.StreamReader( response.GetResponseStream());
            string toreturn = reader.ReadToEnd();

            return toreturn;

And that’s all for the moment folks. Until I do the second post happy hacking!!

Post a Comment

Your Name:*  OR Screen Name:
Email Address:*
Post your Comment

    Note. All comments are sent to a moderator for approval prior to appearing on this page.

    © 2012 Saxon Systems. All rights reserved.Terms of Use  Privacy Policy  Contacts