title: What is the prefered or accepted method for testing proxy settings?
body: <p>I have a lot of trouble with the internet connectivity in the program I am working on and it all seems to spawn from some issue with the proxy settings. Most of the issues at this point are fixed, but the issue I am having now is that my method of testing the proxy settings makes some users wait for long periods of time.</p>
  
  <p>Here is what I do:</p>
  
  <pre><code>System.Net.WebClient webClnt = new System.Net.WebClient();
  
  webClnt.Proxy = proxy;
  webClnt.Credentials = proxy.Credentials;
  
  byte[] tempBytes;
  try
  {
      tempBytes = webClnt.DownloadData(url.Address);
  }
  catch
  {
      //Invalid proxy settings
  
      //Code to handle the exception goes here
  }
  </code></pre>
  
  <p>This is the only way that I've found to test if the proxy settings are correct. I tried making a web service call to our web service, but no proxy settings are needed when making the call. It will work even if I have bogus proxy settings. The above method, though, has no timeout member that I can set that I can find and I use the DownloadData as opposed to the DownloadDataAsync because I need to wait til the method is done so that I can know if the settings are correct before continuing on in the program.</p>
  
  <p>Any suggestions on a better method or a work around for this method is appreciated.</p>
  
  <p>Mike</p>
  
  <p>EDIT: I tried something else, but no luck. I used the DownloadDataAsync method to download the data in a separate thread which raises the DownloadDataCompleted event of the WebClient when finished. While I wait for the event to get called I have a loop: while(DateTime.Now &lt; downloadStart.AddMinutes(timeout) &amp;&amp; !TestIsDone) {} The DownloadDataCompleted event sets the TestIsDone member to true when the event is called. The problem here is if the proxy settings are bad the Event never gets called, no exception is thrown, and the program waits for the entire timeout period before continuing. Here is the code for this approach:</p>
  
  <pre><code>public static bool TestProxy(System.Net.WebProxy proxy)
  {
      ProxySettingsTestDone = false; //public static var
      string address = //url to some arbitrary data on our server
  
      System.Net.WebClient webClnt = new System.Net.WebClient();
  
      webClnt.Proxy = proxy;
      webClnt.Credentials = proxy.Credentials;
  
      try
      {
          webClnt.DownloadDataCompleted += new System.Net.DownloadDataCompletedEventHandler(DownloadDataCallback);
          webClnt.DownloadDataAsync(new Uri(address));
  
          //Timeout period
          DateTime dnldStartTime = DateTime.Now;
          while (DateTime.Now &lt; dnldStartTime.AddMinutes(1.0) &amp;&amp; !ProxySettingsTestDone)
          { }
  
          if (!ProxySettingsTestDone) //Exceded timeout
          {
              throw new System.Net.WebException("Invalid Proxy Settings");
          }
      }
      catch (System.Net.WebException e)
      {
          if (e.Status == System.Net.WebExceptionStatus.ProxyNameResolutionFailure)
          {
              //Proxy failed, server may or may not be there
              Util.ConnectivityErrorMsg = e.Message;
              return false;
          }
          else if (e.Status == System.Net.WebExceptionStatus.ProtocolError)
          {
              //File not found, server is down, but proxy settings succeded
              ServerUp = false;
              Util.ConnectivityErrorMsg = e.Message;
              return true;
          }
  
          return false;
      }
  
      Util.ConnectivityErrorMsg = "";
      return true;
  }
  
  private static void DownloadDataCallback(object sender, System.Net.DownloadDataCompletedEventArgs e)
  {
      if (!e.Cancelled &amp;&amp; e.Error == null)
          ProxySettingsTestDone = true;
      else
          throw new System.Net.WebException("Invalid Proxy Settings");
  }
  </code></pre>
  
  <p>Sorry about the long post. I wanted to update this question with the information that I found after testing this new approach.</p>
  
  <p>Thanks,
  Mike</p>