<分区>
我正在尝试正确处理两个不同的 WebException
。
基本上它们是在调用 WebClient.DownloadFile(string address, string fileName)
之后处理的
AFAIK,到目前为止我必须处理两个,都是 WebException
的:
- 无法解析远程名称(即没有网络连接来访问服务器以下载文件)
- (404) 文件不是名词(即文件在服务器上不存在)
可能还有更多,但这是迄今为止我发现最重要的。
那么我应该如何正确处理这个问题,因为它们都是 WebException
的,但我想以不同的方式处理上述每种情况。
这是我目前所拥有的:
try
{
using (var client = new WebClient())
{
client.DownloadFile("...");
}
}
catch(InvalidOperationException ioEx)
{
if (ioEx is WebException)
{
if (ioEx.Message.Contains("404")
{
//handle 404
}
if (ioEx.Message.Contains("remote name could not")
{
//handle file doesn't exist
}
}
}
如您所见,我正在检查消息以查看它是什么类型的 WebException
。我会假设有更好或更精确的方法来做到这一点?