469,342 Members | 6,660 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,342 developers. It's quick & easy.

C# .Net 2.0 Automatic Configuration of proxy?

84
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#S3

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.
Feb 10 '10 #1

✓ answered by alag20

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.


Expand|Select|Wrap|Line Numbers
  1.  
  2. webRequest.Credentials = CredentialCache.DefaultCredentials;
  3.                     if (WebRequest.DefaultWebProxy != null)
  4.                     {
  5.                         webRequest.Proxy = WebRequest.DefaultWebProxy;
  6.                         webRequest.Credentials = CredentialCache.DefaultCredentials;
  7.                         webRequest.Proxy.Credentials = CredentialCache.DefaultNetworkCredentials;
  8.                     }
  9.                     else
  10.                     {
  11.                         log.Log(LogFileName, "Unable to detect proxy.");
  12.                     }
  13.  
  14.  
The above code segment makes sure that it reads the proxy automatically. You need to set Default Credentials and DefaultNetworkCredentials, 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.

4 5053
alag20
84
Any idea guys? i am still struggling with this.
Feb 11 '10 #2
sashi
1,754 Expert 1GB
Hi Alag20,

Kindly refer to below code segment :)

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3.  public string GetProxyServerName()
  4.  {    
  5.      Net.WebProxy UseProxy = new Net.WebProxy();
  6.      try {
  7.          //if no proxy is specified, an exception is
  8.          //thrown by the frameworks and must be caught
  9.  
  10.          return UseProxy.GetDefaultProxy.Address.Host;
  11.      }
  12.      catch {
  13.          //catch the error when no proxy is specified in IE                    
  14.          return "Not Specified";        
  15.      }
  16.  }
  17.  
  18.  
  19.  public string GetProxyServerPort()
  20.  {    
  21.      Net.WebProxy UseProxy = new Net.WebProxy();
  22.  
  23.      try {
  24.          //if no proxy is specified, an exception is
  25.          //thrown by the frameworks and must be caught                    
  26.          return UseProxy.GetDefaultProxy.Address.Port;
  27.      }
  28.      catch {
  29.          //catch the error when no proxy is specified in IE            
  30.          return "Not Specified";        
  31.      }
  32. }
  33.  
Feb 13 '10 #3
alag20
84
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.


Expand|Select|Wrap|Line Numbers
  1.  
  2. webRequest.Credentials = CredentialCache.DefaultCredentials;
  3.                     if (WebRequest.DefaultWebProxy != null)
  4.                     {
  5.                         webRequest.Proxy = WebRequest.DefaultWebProxy;
  6.                         webRequest.Credentials = CredentialCache.DefaultCredentials;
  7.                         webRequest.Proxy.Credentials = CredentialCache.DefaultNetworkCredentials;
  8.                     }
  9.                     else
  10.                     {
  11.                         log.Log(LogFileName, "Unable to detect proxy.");
  12.                     }
  13.  
  14.  
The above code segment makes sure that it reads the proxy automatically. You need to set Default Credentials and DefaultNetworkCredentials, 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.
Feb 18 '10 #4
sashi
1,754 Expert 1GB
Hi Alag,

Many thanks for sharing :)
Feb 18 '10 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by Hallvard B Furuseth | last post: by
4 posts views Thread by sebsauvage | last post: by
1 post views Thread by Tim Heuer | last post: by
3 posts views Thread by hB | last post: by
7 posts views Thread by Mike Livenspargar | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by haryvincent176 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.