How to check if a proxy server is configured?

I have code that works fine when I have web proxies defined in Internet Explorer. However, if it is not defined, it does not work. I want to check if a proxy is defined. How to change the code below to do this?

public DataTable GetCurrentFxPrices(string url) { WebProxy wp = new WebProxy(WebProxy.GetDefaultProxy().Address.AbsoluteUri, true); wp.Credentials = CredentialCache.DefaultCredentials; WebClient wc = new WebClient(); wc.Proxy = wp; MemoryStream ms = new MemoryStream(wc.DownloadData(url)); DataSet ds = new DataSet("fxPrices"); ds.ReadXml(ms); DataTable dt = ds.Tables["Rate"]; int i = dt.Rows.Count; return dt; } 

For example, how to download data without using a proxy?

UPDATE

I changed the code to the next

 public DataTable GetCurrentFxPrices(string url) { WebClient wc = new WebClient(); if (!String.IsNullOrEmpty(WebProxy.GetDefaultProxy().Address.AbsoluteUri)) { WebProxy wp = new WebProxy(WebProxy.GetDefaultProxy().Address.AbsoluteUri, true); wp.Credentials = CredentialCache.DefaultCredentials; wc.Proxy = wp; } MemoryStream ms = new MemoryStream(wc.DownloadData(url)); DataSet ds = new DataSet("fxPrices"); ds.ReadXml(ms); DataTable dt = ds.Tables["Rate"]; int i = dt.Rows.Count; return dt; } 

I get the following System.NullReferenceException was unhandled by user code error System.NullReferenceException was unhandled by user code in the if statement line.

UPDATE 2

I also tried changing this line:

if (!String.IsNullOrEmpty(WebProxy.GetDefaultProxy().Address.AbsoluteUri))

to

if (WebProxy.GetDefaultProxy().Address.AbsoluteUri != null)

but I get this error:

System.NullReferenceException: Object reference not set to an instance of an object.

Any ideas?

+6
c # proxy
source share
3 answers

Remember that there is no one “proxy address” or Uri proxy, as you think. Instead, the Uri proxy may depend on each Uri retrieved, as seen in the Internet Explorer proxy settings dialog box.

Internet Explorer - Proxy Settings dialog

The IWebProxy interface helps you get the right Uri proxy and tells you whether this proxy will be used or bypassed to get a specific Uri.

 using System.Net; Uri exampleUri = new Uri("http://www.example.org/") IWebProxy defaultProxy = WebRequest.GetSystemWebProxy(); bool isBypassed = defaultProxy.IsBypassed(exampleUri); // ... false Uri proxyUri = defaultProxy.GetProxy(exampleUri); // ... http://someproxy.mycorp.example:8080 

In your method, you will need to pass the IWebProxy interface, not the proxy address. By default, a proxy interface is used (for example, GetSystemWebProxy ).

If you want to install your own proxy server in case there is no proxy server used for your Uri, you can do the following ...

 public DataTable GetCurrentFxPrices(string url) { Uri uri = new Uri(url); WebClient webClient = new WebClient(); IWebProxy defaultProxy = WebRequest.GetSystemWebProxy(); IWebProxy myProxy = new WebProxy(new Uri("http://myproxy:8080")) // if no bypass-list is specified, all Uris are to be retrieved via proxy if (defaultProxy.IsBypassed(uri)) { myProxy.Credentials = CredentialCache.DefaultCredentials; webClient.Proxy = myProxy; } MemoryStream ms = new MemoryStream(webClient.DownloadData(url)); DataSet ds = new DataSet("fxPrices"); ds.ReadXml(ms); DataTable dt = ds.Tables["Rate"]; int i = dt.Rows.Count; return dt; } 
+10
source share

Just call

 if(!String.IsNullOrEmpty(WebProxy.GetDefaultProxy().Address.AbsoluteUri)) { //do something you want if proxy is set } else { //something else(proxy not set) } 
0
source share

Is it possible to use a debugger and set a breakpoint in an if statement?

If I'm right, calling WebProxy.GetDefaultProxy() returns null and therefore a NullReferenceException .

What happens if you change the code to:

 if ((WebProxy.GetDefaultProxy() != null) && (!String.IsNullOrEmpty(WebProxy.GetDefaultProxy().Address.AbsoluteUri))) 

I think it should solve your problem.

0
source share

All Articles