I am working on a App that get data from remote server by using WebClient class. The problem is I cannot distinguish the error is:
(1) Connection timeout
(2) The URL doesn’t exist
(3) No network connection
Here is the code snippet:
WebClient client = new WebClient();
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
client.DownloadStringAsync(new Uri(url, UriKind.Absolute));
void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error == null && !e.Cancelled)
{
}
else
{
MessageBox.Show(e.Error.ToString()); // Return System.Net.WebException
MessageBox.Show(e.Error.Data.Count + ""); // return 0
/*foreach (DictionaryEntry de in e.Error.Data)
MessageBox.Show(de.Key + ", " + de.Value);*/
}
}
I need to know the error type of DownloadStringCompletedEventArgs as I am going to display a customized error message to user. Please help, thanks!
You can make use of
e.Error.Message, which gives the specific error messageBased on your comment:
I didn’t find any resource which gives the list of all error messages. Check the following:
AsyncCompletedEventArgs.Error and
Exception.Message