Reputation: 7228
I have some code that runs fine when I have a web proxy defined in Internet Explorer. However if there is none defined it doesn't work. I want to check if the a proxy is defined. How would I change the below code to do that?
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 would I download the data without using a proxy?
UPDATE
I have changed the code to the following
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 error System.NullReferenceException was unhandled by user code
on the if statement line.
UPDATE 2
I've 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?
Upvotes: 7
Views: 14909
Reputation: 20175
Just call
if(!String.IsNullOrEmpty(WebProxy.GetDefaultProxy().Address.AbsoluteUri))
{
//do something you want if proxy is set
}
else
{
//something else(proxy not set)
}
Upvotes: 0
Reputation: 2012
Remember, there is not one single "proxy address" or proxy Uri as you might think. Instead the proxy Uri may depend on each Uri to be retrieved, as can be seen in Internet Explorer's Proxy Settings dialog.
The IWebProxy interface helps you to get the right proxy Uri, and tells you if this proxy will be used or bypassed for a specific Uri to be retrieved.
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 would have to pass an IWebProxy interface, not a proxy address. The default proxy interface (like from GetSystemWebProxy) is always set as a default.
If you want to set your own special proxy in case there is no proxy used for your Uri, you could 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;
}
Upvotes: 11
Reputation: 2794
Can you use the debugger and put a breakpoint on the if statement?
If I'm correct the WebProxy.GetDefaultProxy()
call will return null and hence the 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.
Upvotes: 0