webclient 的 DownloadDataAsync异常捕获问题 请大家帮忙看看 先感谢
这个函数是获取公网 IP地址的。异常捕捉不到,比如我把网卡禁用,执行上面代码,出现系统崩溃的那种异常提示。 这个地方到底如何捕获异常啊
public string GetExternalIP()
{
string whatIsMyIp = "http://automation.whatismyip.com/n09230945.asp";
WebClient wc = new WebClient();
UTF8Encoding utf8 = new UTF8Encoding();
try
{
string ipaddr = null;
bool done = false;
wc.DownloadDataCompleted += new
DownloadDataCompletedEventHandler((object sender,
DownloadDataCompletedEventArgs e) =>
{
ipaddr = utf8.GetString(e.Result);
done = true;
});
wc.DownloadDataAsync(new Uri(whatIsMyIp));
return ipaddr;
}
catch
{
return null;
}
finally
{
if (wc != null)
{
wc.Dispose();
wc = null;
}
}
}