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?
Mark allison
source share