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

pc2000 pc2000 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
这个函数是获取公网 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;
}
}
}



...全文

相关推荐

评论 2
收藏 已收藏
分享
返回顶部
pc2000
C# 111,128 成员 642,537 内容