下载文件时如何处理404未找到的异常?

时间:2015-02-16 21:46:26

标签: c# .net winforms

这是我用来下载文件的方法。 我的程序正在使用计时器,每15分钟就会尝试下载文件:

HttpWebRequest request;
        int currentIndex = 0;
        void fileDownloadRadar(string uri, string fileName)
        {
            if (splash != null)
            {
                if (!splash.IsDisposed)
                    splash.UpdateProgressBar(0);
            }
            try
            {
                request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(uri);
                request.ContentType = "text/html";
                request.CookieContainer = new CookieContainer();
                request.AllowAutoRedirect = true;
                request.Timeout = 10000;
                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {

                    long contentLength = response.ContentLength;
                    if (response.ContentType == "")
                    {
                        Logger.Write("ContentType is Empty download was not fine !!!!!");
                    }
                    if ((response.StatusCode == HttpStatusCode.OK ||
                        response.StatusCode == HttpStatusCode.Moved ||
                        response.StatusCode == HttpStatusCode.Redirect) &&
                        response.ContentType.StartsWith("image", StringComparison.OrdinalIgnoreCase))
                    {
                        Logger.Write("ContentType is not empty meaning download is fine");
                        using (Stream inputStream = response.GetResponseStream())
                        using (Stream outputStream = File.OpenWrite(fileName))
                        {
                            inputStream.ReadTimeout = 10000;
                            inputStream.WriteTimeout = 10000;
                            byte[] buffer = new byte[4096];
                            int bytesRead;
                            do
                            {
                                bytesRead = inputStream.Read(buffer, 0, buffer.Length);
                                currentIndex += bytesRead;
                                double percentage = (double)currentIndex / contentLength;
                                if (splash != null)
                                {
                                    if (!splash.IsDisposed)
                                        splash.UpdateProgressBar((int)(percentage * 100));
                                }
                                outputStream.Write(buffer, 0, bytesRead);
                            } while (bytesRead != 0);
                            if (splash != null)
                            {
                                if (!splash.IsDisposed)
                                {
                                    splash.UpdateProgressBar(100);
                                }
                            }
                        }

                    }
                    else
                    {
                        timer1.Stop();
                        timer3.Start();
                    }
                    if (splash == null)
                        FinishWebRequest();
                }
            }
            catch (Exception ex)
            {                
                  Logger.Write(ex.ToString());
            }
        }

该程序运行正常几个小时,然后大约30分钟前它将这个异常抛到了一行:

using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())

WebException:远程服务器返回错误:(404)Not Found

完整的异常消息:

System.Net.WebException occurred
  HResult=-2146233079
  Message=The remote server returned an error: (404) Not Found.
  Source=System
  StackTrace:
       at System.Net.HttpWebRequest.GetResponse()
       at mws.Form1.fileDownloadRadar(String uri, String fileName) in d:\C-Sharp\Download File\Downloading-File-Project-Version-012\Downloading File\Form1.cs:line 1015
  InnerException:

第1015行是:

using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())

它没有跳转到catch部分只是在这一行抛出异常。 我的问题我该如何处理这个案子?

也许,我想在这个例外的情况下做一个方法,试图下载文件,如每次30秒尝试下载15次,如果15次没下载,则继续下一个15分钟。但我不确定这是否是一个很好的解决方案,无论如何我应该如何处理异常?我添加了尝试和捕获,但它没有得到捕获。

1 个答案:

答案 0 :(得分:2)

A'未找到'错误(404)可能有很多原因。有些你永远无法恢复(页面不在那里,永远不会)。您可能能够从中恢复的一些原因,例如登录。

处理它的最佳方法取决于你对它的使用(可以等待吗?)和上述原因。没有一条黄金法则,你必须找到自己。

首先确定为什么出错。然后看看如何处理它。