【发布时间】:2012-01-23 08:06:40
【问题描述】:
我正在编写一个下载到文件的程序。第二个文件不是必需的,只是在某些时候被包括在内。如果不包含第二个文件,它将返回 HTTP 404 错误。
现在,问题是当返回此错误时,它会结束整个程序。我想要的是继续程序并忽略 HTTP 错误。所以,我的问题是如何从WebClient.DownloadFile 请求中捕获HTTP 404 错误?
这是当前使用的代码::
WebClient downloader = new WebClient();
foreach (string[] i in textList)
{
String[] fileInfo = i;
string videoName = fileInfo[0];
string videoDesc = fileInfo[1];
string videoAddress = fileInfo[2];
string imgAddress = fileInfo[3];
string source = fileInfo[5];
string folder = folderBuilder(path, videoName);
string infoFile = folder + '\\' + removeFileType(retrieveFileName(videoAddress)) + @".txt";
string videoPath = folder + '\\' + retrieveFileName(videoAddress);
string imgPath = folder + '\\' + retrieveFileName(imgAddress);
System.IO.Directory.CreateDirectory(folder);
buildInfo(videoName, videoDesc, source, infoFile);
textBox1.Text = textBox1.Text + @"begining download of files for" + videoName;
downloader.DownloadFile(videoAddress, videoPath);
textBox1.Text = textBox1.Text + @"Complete video for" + videoName;
downloader.DownloadFile(imgAddress, imgPath);
textBox1.Text = textBox1.Text + @"Complete img for" + videoName;
}
【问题讨论】: