【问题标题】:Downloading files asynchronously from httpwebrequest throws timeout error从 httpwebrequest 异步下载文件会引发超时错误
【发布时间】:2014-10-04 01:22:36
【问题描述】:

我有以下方法使用 httpwebrequest 从网址下载文件。我正在下载列表中包含的 150 个文件。这可能需要最多 30 分钟。当我运行我的服务时,我的 webrequests 不断超时,我不知道为什么?我猜它正在创建 150 个任务并尽可能多地处理,但在某些任务的超时期限之后超时。我的方法发生了什么?

try
{
     //Download File here
     Task t = new Task(() =>
     {
         byte[] lnBuffer;
         byte[] lnFile;
         HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(string.Format("{0}/{1}", Settings1.Default.WebPhotosLocation, f.FileName));
//Breaks on the line below
         using (HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse())
         {
              httpWebRequest.Timeout = 14400000;
              httpWebRequest.KeepAlive = true;
              using (BinaryReader binaryReader = new BinaryReader(httpWebResponse.GetResponseStream()))
              {
                    using (MemoryStream memoryStream = new MemoryStream())
                    {
                          lnBuffer = binaryReader.ReadBytes(1024);
                          while (lnBuffer.Length > 0)
                          {
                                memoryStream.Write(lnBuffer, 0, lnBuffer.Length);
                                lnBuffer = binaryReader.ReadBytes(1024);
                          }
                          lnFile = new byte[(int)memoryStream.Length];
                          memoryStream.Position = 0;
                          memoryStream.Read(lnFile, 0, lnFile.Length);
                    }
               }
         }
         using (System.IO.FileStream lxFS = new FileStream(string.Format(@"{0}\{1}", fileDirectory, f.FileName), FileMode.Create))
         {
               lxFS.Write(lnFile, 0, lnFile.Length);
         }
         Log.WriteLine(string.Format("Downloaded File: {0}", f.FullName), Log.Status.Success);
         filesDownloaded++;
     });
     t.Start();
     listOfTasks.Add(t);
}
catch (Exception ex)
{
     Log.WriteLine(string.Format("There has been an error Downloading File {0}. Error: {1}", f.FullName, ex), Log.Status.Error);
     throw;
}

编辑

在代码中声明的行抛出异常:

异常:“System.Net.WebException”类型的异常发生在 System.dll,但未在用户代码中处理

附加信息:操作已超时

第一次下载开始后 3 分 20 秒后抛出错误

【问题讨论】:

  • 你是怎么调用这个方法的?您确定您的服务器可以同时处理 150 个调用吗?更重要的是,您不需要线程来执行 IO 绑定工作。
  • 我在客户端的 foreach 循环中调用此方法。我什至不确定如何检查我的服务器是否可以进行 150 个并发调用,以及为什么我不需要线程来执行 IO 工作?我以为我的线程会在每次下载后等待下一次下载?
  • 旁注:将所有复制算法替换为Stream.Copy,直接替换为FileStream。或:new WebClient().DownloadFile(url, path).
  • 连接限制设置为多少?不是默认2吗?!
  • 我从Here 获得了我的下载算法,但我会看看DownloadFile 方法。连接限制是设置服务器端还是客户端?当我的程序超时时,大约 2 分钟的声音

标签: c# asynchronous httpwebrequest task-parallel-library task


【解决方案1】:

原来我在调用 GetResponse 后设置了超时值。将超时设置在此方法调用之上效果很好。

HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(string.Format("{0}/{1}", Settings1.Default.WebPhotosLocation, f.FileName));
httpWebRequest.Timeout = 14400000;
httpWebRequest.KeepAlive = true;
using (HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse())
{
    //Do stuff
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-08
    • 1970-01-01
    相关资源
    最近更新 更多