C# .Net 2.0 Automatic Configuration of proxy?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • alag20
    New Member
    • Apr 2007
    • 84

    C# .Net 2.0 Automatic Configuration of proxy?

    Hi Guys, I am working on a project to download files using .Net c# 2.0 SP1. One of the requirements of the project is to not seek any proxy details and automatically detect this from IE etc.

    Now i haven't worked with proxy before and hence i am a bit baffled. After hours of searching google, msdn etc, i have come across the following article http://msdn.microsoft. com/en-gb/magazine/cc300743.aspx#S 3

    Now i am totally confused how to do this? Can someone please help me with an easier implementation or guide me please?

    Sorry once again if i sound silly, but i haven't worked with .Net proxies and hence I am struggling.

    Thanks in advance for your help.
  • alag20
    New Member
    • Apr 2007
    • 84

    #2
    Any idea guys? i am still struggling with this.

    Comment

    • sashi
      Recognized Expert Top Contributor
      • Jun 2006
      • 1749

      #3
      Hi Alag20,

      Kindly refer to below code segment :)

      Code:
      
       public string GetProxyServerName()
       {    
           Net.WebProxy UseProxy = new Net.WebProxy();
           try {
               //if no proxy is specified, an exception is
               //thrown by the frameworks and must be caught
                          
               return UseProxy.GetDefaultProxy.Address.Host;
           }
           catch {
               //catch the error when no proxy is specified in IE                    
               return "Not Specified";        
           }
       }
      
      
       public string GetProxyServerPort()
       {    
           Net.WebProxy UseProxy = new Net.WebProxy();
          
           try {
               //if no proxy is specified, an exception is
               //thrown by the frameworks and must be caught                    
               return UseProxy.GetDefaultProxy.Address.Port;
           }
           catch {
               //catch the error when no proxy is specified in IE            
               return "Not Specified";        
           }
      }

      Comment

      • alag20
        New Member
        • Apr 2007
        • 84

        #4
        Hi Sashi,
        Thanks for your kind help below. I fixed this in a slightly different way as below for anyone who may need it in future.


        Code:
        webRequest.Credentials = CredentialCache.DefaultCredentials;
                            if (WebRequest.DefaultWebProxy != null)
                            {
                                webRequest.Proxy = WebRequest.DefaultWebProxy;
                                webRequest.Credentials = CredentialCache.DefaultCredentials;
                                webRequest.Proxy.Credentials = CredentialCache.DefaultNetworkCredentials;
                            }
                            else
                            {
                                log.Log(LogFileName, "Unable to detect proxy.");
                            }
        The above code segment makes sure that it reads the proxy automatically. You need to set Default Credentials and DefaultNetworkC redentials, as without that some proxies wont work, however with that all the proxies should work unless you need to give different credentials which you can provide.

        Comment

        • sashi
          Recognized Expert Top Contributor
          • Jun 2006
          • 1749

          #5
          Hi Alag,

          Many thanks for sharing :)

          Comment

          Working...