Exception handling the right way for WebClient.DownloadString

If you catch WebException, it should handle most cases. WebClient and HttpWebRequest throw a WebException for all HTTP protocol errors (4xx and 5xx), and also for network level errors (disconnection, host not reachable, etc)


How do I capture this from the UI to show the error in a message box?

I'm not sure I understand your question... Can't you just show the exception message?

MessageBox.Show(e.Message);

Don't catch the exception in FindUpcomingGamesByPlatform, let it bubble up to the calling method, catch it there and show the message...


I usually handle it like this to print any exception message the remote server is returning. Given that the users are allowed to see that value.

try
{
    getResult = client.DownloadString(address);
}
catch (WebException ex)
{
    String responseFromServer = ex.Message.ToString() + " ";
    if (ex.Response != null)
    {
        using (WebResponse response = ex.Response)
        {
            Stream dataRs = response.GetResponseStream();
            using (StreamReader reader = new StreamReader(dataRs))
            {
                responseFromServer += reader.ReadToEnd();
            }
        }
    }
    _log.Error("Server Response: " + responseFromServer);
    MessageBox.Show(responseFromServer);
}

I use this code:

  1. Here I init the webclient whithin the loaded event

    private void LayoutRoot_Loaded(object sender, RoutedEventArgs e)
    {
      // download from web async
      var client = new WebClient();
      client.DownloadStringCompleted += client_DownloadStringCompleted;
      client.DownloadStringAsync(new Uri("http://whateveraurisingis.com"));
    }
    
  2. The callback

    void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
      #region handle download error
      string download = null;
      try
      {
        download = e.Result;
      }
    catch (Exception ex)
      {
        MessageBox.Show(AppMessages.CONNECTION_ERROR_TEXT, AppMessages.CONNECTION_ERROR, MessageBoxButton.OK);
      }
    
      // check if download was successful
      if (download == null)
      {
        return;
      }
      #endregion
    
      // in my example I parse a xml-documend downloaded above      
      // parse downloaded xml-document
      var dataDoc = XDocument.Load(new StringReader(download));
    
      //... your code
    }
    

Thanks.

Tags:

C#

.Net

Webclient