title: How to check if a proxy server is configured?
body: <p>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?</p>
  
  <pre><code>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;
  }
  </code></pre>
  
  <p>For example how would I download the data without using a proxy?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I have changed the code to the following</p>
  
  <pre><code>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;
  }
  </code></pre>
  
  <p>I get the following error <code>System.NullReferenceException was unhandled by user code</code> on the if statement line. </p>
  
  <p><strong>UPDATE 2</strong></p>
  
  <p>I've also tried changing this line:</p>
  
  <p><code>if (!String.IsNullOrEmpty(WebProxy.GetDefaultProxy().Address.AbsoluteUri))</code></p>
  
  <p>to</p>
  
  <p><code>if (WebProxy.GetDefaultProxy().Address.AbsoluteUri != null)</code></p>
  
  <p>but I get this error:</p>
  
  <p><code>System.NullReferenceException: Object reference not set to an instance of an object.</code></p>
  
  <p>Any ideas?</p>