webclient 的 DownloadDataAsync异常捕获问题 请大家帮忙看看 先感谢

pc2000 2012-11-13 01:21:11
这个函数是获取公网 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;
}
}
}



...全文
135 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
pc2000 2012-11-15
  • 打赏
  • 举报
回复
楼上的还是不行啊
心灵彩虹 2012-11-13
  • 打赏
  • 举报
回复
catch(Exception e) { message.show(e.tostring) }

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧