473,386 Members | 1,668 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 5299
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

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

Similar topics

4
by: Hallvard B Furuseth | last post by:
Is it possible to write a metaclass or something so that with a class definition not much larger than this: class foo: def __init__(self, val): self.val = val operations on a foo instance f...
4
by: sebsauvage | last post by:
Hello. In one of my programs ( http://sebsauvage.net/python/webgobbler/ ), I have a global dictionnary containing the whole program configuration. Sample follows: CONFIG = {...
1
by: Tim Heuer | last post by:
Is there a way to specify proxy logon information if you use the <system.net> <proxyAddress> configuration methods for the web.config? -th
3
by: hB | last post by:
Hi. Error = "The configuration system can only be set once. Configuration system is already set" Exception Details: System.InvalidOperationExcepti­on: The configuration system can only...
2
by: asanford | last post by:
Hello, Is there a way to associate and set the credentials for a web proxy via the config file? We are writing a client program to access both web services (via HttpWebClientProtocol...
7
by: Mike Livenspargar | last post by:
We have an application converted from v1.1 Framework to v2.0. The executable references a class library which in turn has a web reference. The web reference 'URL Behavior' is set to dynamic. We...
0
by: baskarpr | last post by:
In Internet Explorer (I use 6.0 ), there is a check box to enable/disable automatic configuration script. Is there any way to check /uncheck this option using Win API ( that can be implemented in VB...
1
by: Rajen | last post by:
I've a automatic configuration proxy (http://xxx.xxx.xxx.xxx/xyz.pac) which I use in the Internet explorer Lan Settings to access the net. How do I read a webpage by specifying this proxy in my...
3
by: Lance Wynn | last post by:
Hello, I am receiving this error when trying to instantiate a webservice component. I have 2 development machines, both are XP sp2 with VS 2008 installed. On one machine, the code works fine. On...
8
by: Bill McCormick | last post by:
<!-- When deploying the service library project, the content of the config file must be added to the host's app.config file. System.Configuration does not support config files for libraries. --> ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.