如何检查WebClient请求是否有404错误

Ale*_*tti 18 c# webclient

我有一个程序,我写的是下载到文件.第二个文件不是必要的,只包括一些时间.如果未包含第二个文件,则会返回HTTP 404错误.

现在,问题是当返回此错误时,它将结束整个程序.我想要的是继续该程序并忽略HTTP错误.所以,我的问题是如何HTTP 404WebClient.DownloadFile请求中捕获错误?

这是当前使用的代码::

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;
}
Run Code Online (Sandbox Code Playgroud)

Ian*_*emp 20

如果您特别想要捕获错误404:

using (var client = new WebClient())
{
  try
  {
    client.DownloadFile(url, destination);
  }
  catch (WebException wex)
  {
    if (((HttpWebResponse) wex.Response).StatusCode == HttpStatusCode.NotFound)
    {
      // error 404, do what you need to do
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

  • 这应该是公认的答案。获取实际的`HttpWebResponse.StatusCode`枚举值比在`string`消息中查找代码要好得多。 (3认同)

Joh*_*han 12

WebClient将为所有4xx和5xx响应抛出WebException.

try {
    downloader.DownloadFile(videoAddress, videoPath);
}
catch (WebException ex) {
    // handle it here
}
Run Code Online (Sandbox Code Playgroud)


Ama*_*ure 7

放入try catch你的foreach循环.

 foreach (string[] i in textList)
 {
    try
    {
        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;
        if(Download(videoAddress, videoPath) == false)
        {
           //Download failed. Do what you want to do.
        }
        textBox1.Text = textBox1.Text + @"Complete video for" + videoName;
        if(Download(imgAddress, imgPath)== false)
        {
           //Download failed. Do what you want to do.
        }
        textBox1.Text = textBox1.Text + @"Complete img for" + videoName;
    }
    catch(Exception ex)
    {
        //Error like IO Exceptions, Security Errors can be handle here. You can log it if you want.
    }
 }
Run Code Online (Sandbox Code Playgroud)

私有函数下载文件

 private bool Download(string url, string destination)
 {
     try
     {
         WebClient downloader = new WebClient();
         downloader.DownloadFile(url, destination);
         return true;
     }
     catch(WebException webEx)
     {
        //Check (HttpWebResponse)webEx.Response).StatusCode
        // Or
        //Check for webEx.Status
     }
     return false;
 }
Run Code Online (Sandbox Code Playgroud)

您可以查看WebException状态.根据错误代码,您可以继续或中断.

阅读更多@ MSDN

建议

希望这对你有用.


小智 1

try catch WebException在代码中使用 a并检查Exception消息 - 它将包含 http StatusCode

您可以清除Exception并继续。