我正在开发一个使用 WebClient 类从远程服务器获取数据的应用程序。问题是我无法区分错误是:(1)连接超时(2)URL不存在(3)没有网络连接
这是代码片段:
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);*/
}
}
我需要知道 DownloadStringCompletedEventArgs 的错误类型,因为我要向用户显示自定义错误消息。请帮忙,谢谢!