【发布时间】:2013-04-29 13:38:38
【问题描述】:
这段代码:
try
{
_wcl.DownloadFile(url, currentFileName);
}
catch (WebException ex)
{
if (ex.Status == WebExceptionStatus.ProtocolError && ex.Response != null)
if ((ex.Response as HttpWebResponse).StatusCode == HttpStatusCode.NotFound)
Console.WriteLine("\r{0} not found. ", currentFileName);
}
下载文件并通知是否发生 404 错误。
我决定异步下载文件:
try
{
_wcl.DownloadFileAsync(new Uri(url), currentFileName);
}
catch (WebException ex)
{
if (ex.Status == WebExceptionStatus.ProtocolError && ex.Response != null)
if ((ex.Response as HttpWebResponse).StatusCode == HttpStatusCode.NotFound)
Console.WriteLine("\r{0} not found. ", currentFileName);
}
现在如果服务器返回 404 错误并且 WebClient 生成一个空文件,则此 catch 块不会触发。
【问题讨论】:
标签: c# .net asynchronous http-status-code-404 webclient