【问题标题】:DownloadFile stopped downloading fileDownloadFile 停止下载文件
【发布时间】:2019-08-20 07:35:23
【问题描述】:

我有一个更新应用程序,当有可用更新时它正在启动。此应用程序只是将新的 exe 下载到指定路径,但突然它不再工作了。更新程序下载的文件大小为0kb,没有报错。

我在 2 个月前将新的 exe 上传到服务器,许多客户成功下载了该文件。昨天,我的一位客户注意到他开始使用该应用程序并且更新失败。更新程序在许多客户端上运行并且始终有效。会不会是服务器问题?

这是 C# 中的更新程序代码:

public void StartUpdate()
{
    WebClient webclient = new WebClient();
    try
    {
        //webclient.DownloadFile("http://www.example.nl/folder/example.exe", @"C:\example\example.exe");

        webclient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(webclient_DownloadProgressChanged);
        webclient.DownloadFileCompleted += new AsyncCompletedEventHandler(webclient_DownloadFileCompleted);
        webclient.DownloadFileAsync(new Uri("http://www.example.nl/folder/example.exe"), @"C:\example\example.exe");
    }
    catch (Exception)
    {
        MessageBox.Show("Download Failed.\n\nPlease contact your system administrator");
        Application.Exit();
    }
}

void webclient_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
    label1.Text = "Download successfully!";
    label3.Text = "Download complete!";
    timer2.Enabled = true; //here some other magic happens like start the program and exit this updater.
}

void webclient_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
    progressBar1.Maximum = (int)e.TotalBytesToReceive / 100;
    progressBar1.Value = (int)e.BytesReceived / 100;
}

我在存储 exe 文件的 CentOS 上运行 Apache。文件夹/文件权限没问题。当我在任何浏览器中打开 exe URL 时,文件已成功下载。 在过去的 2 个月内,我从未更改过 exe 文件,也从未更改过网络服务器上的任何其他设置。这个方法用了2年,现在自动失效了。

更新:

System.Net.WebException: The request has been aborted: Cannot create a secure SSL / TLS channel.
 at System.Net.HttpWebRequest.EndGetResponse (IAsyncResult asyncResult)
 at System.Net.WebClient.GetWebResponse (WebRequest request, IAsyncResult result)
 at System.Net.WebClient.DownloadBitsResponseCallback (IAsyncResult result) A first chance exception or type 'System.ComponentModel.Win32Exception' occurred in System.dll

【问题讨论】:

  • 我在不同的机器上测试了这个,它不再在所有机器上工作了......
  • 我敢打赌服务器现在需要 TLS1.2。这并不是什么新鲜事,自 2016 年以来每个人都在转向 TLS1.2 和 HeartBleed。早在 2016 年,航空公司就放弃了对低于 TLS1.2 的任何东西的支持。谷歌、Azure、亚马逊、银行等已经宣布他们多年来将放弃对低于 TLS1.2 的任何东西的支持。

标签: c# webclient downloadfile downloadfileasync


【解决方案1】:

程序无法处理安全的 uri。我添加了以下行

System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;

这条线以下

webclient.DownloadFileCompleted += new AsyncCompletedEventHandler(webclient_DownloadFileCompleted);

解决了我的问题。

【讨论】:

  • 这根本不是问题。问题是 服务器 放弃了对旧 TLS 协议的支持,这是过去 3 年所有服务提供商和公司都在发生的事情。
  • 我现在的另一个担心是我被卡住了,因为我不知道如何更新每台客户端机器上的所有更新程序:( 建议任何建议。
  • 正确解决方案是使用当前的 .NET 版本和 Windows 操作系统。自 4.6.1 以来,.NET 在 SSL 协商期间使用操作系统上可用的最佳算法。这意味着当 TLS1.3 在操作系统上可用时,.NET 将自动获取它。仅在较旧的 .NET 版本中才需要对 TLS 版本进行硬编码。
  • Windows 2008 R2 等较旧的操作系统版本也需要打补丁才能默认支持 TLS1.2。这很重要,因为人们正准备迁移到 TLS1.3。硬编码 TLS1.2 可能会在不久的将来出现问题
【解决方案2】:

您的服务器 SSL 证书似乎已损坏。有可能过期了。如果您使用的是自签名证书,请确保您导入了用于自签名服务器证书的 CA 证书。另一种可能性是您的服务器(或客户端)的系统时钟无效。所以客户认为你的证书过期了。

【讨论】:

  • 这个方法工作了2年,现在自动停止工作了。很明显,这就是问题所在......
  • @Sefe 我敢打赌问题是“我们多年来一直忽略 SSL/TLS1.0 弃用,现在我们无法连接”
猜你喜欢
  • 1970-01-01
  • 2011-01-01
  • 2016-06-08
  • 2018-03-28
  • 1970-01-01
  • 2013-07-05
  • 1970-01-01
  • 1970-01-01
  • 2014-09-22
相关资源
最近更新 更多