【问题标题】:Download Handler fails to download file from GitHub URL下载处理程序无法从 GitHub URL 下载文件
【发布时间】:2018-08-10 06:24:05
【问题描述】:

我的程序有一个更新按钮,可以从 GitHub 下载最新版本的 zip。

它工作了好几个月,但现在进度条不再更新,也没有从 GitHub 发布 URL 下载文件。

但是,我使用 Google Drive URL 对其进行了测试,它可以工作,但 URL 命名约定与此项目不兼容。

有没有办法通过代码绕过它,还是这与 GitHub 服务器规则有关?


下载处理程序

// Progress Changed
//
public void wc_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
    // Progress Bar
    this.Dispatcher.BeginInvoke((Action)(() =>
    {
        double bytesIn = double.Parse(e.BytesReceived.ToString());
        double totalBytes = double.Parse(e.TotalBytesToReceive.ToString());
        double percentage = bytesIn / totalBytes * 100;
        this.progressBar.Value = int.Parse(Math.Truncate(percentage).ToString());
    }));
}


// Download Complete
//
public void wc_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
    // Set the waiter Release
    // Must be here
    this.Dispatcher.BeginInvoke((Action)(() =>
    {
        waiter.Set();
    }));
}

下载方法

在实际代码中,版本号是一个在URL中动态变化的字符串。

不起作用:
https://github.com/MattMcManis/Glow/releases/download/v0.0.5.4-alpha/Glow.zip

作品:
https://drive.google.com/uc?authuser=0&id=1_7hzLR4FFZXK6qr2sP0uOleeB2GgtghE&export=download

public void StartDownload()
{
    // Start New Thread
    Thread worker = new Thread(() =>
    {
        waiter = new ManualResetEvent(false);

        Uri downloadUrl = new Uri("https://github.com/MattMcManis/Glow/releases/download/v0.0.5.4-alpha/Glow.zip);

        //Async
        wc.DownloadProgressChanged += new DownloadProgressChangedEventHandler(wc_DownloadProgressChanged);
        wc.DownloadFileCompleted += new AsyncCompletedEventHandler(wc_DownloadFileCompleted);
        wc.DownloadFileAsync(downloadUrl, tempDir + "Glow.zip");

        // Wait for Download to finish
        waiter.WaitOne();
    });

    // Start Download Thread
    worker.Start();
}

【问题讨论】:

  • wc 定义/初始化在哪里?
  • @RonBeyer 在 UpdateWindow 类中。在顶部,当窗口第一次打开时。 public static WebClient wc = new WebClient();。在InitializeComponent() 之后运行StartDownload();。直到最近,这一切都正常工作。
  • 您也可能在下载过程中遇到异常,导致线程终止。您是否查看过线程视图以查看线程是否仍在运行?
  • @RonBeyer 抛出异常:“对 SSPI 的调用失败,请参阅内部异常。”和“请求被中止:无法创建 SSL/TLS 安全通道。”

标签: c# wpf github


【解决方案1】:

2018 年 2 月 22 日GitHub disabled certain depreciated algorithms。这会导致您遇到的 TLS 错误。

修复(如 cmets 中所述)是仅允许 WebClient 使用 TLS1.2 或更高版本进行连接。为此,请将ServicePointManager.SecurityProtocol 设置为SecurityProtocolType.Tls12 或更高。

我不确定为什么您的进度条在停止之前会显示“已下载一半”,开始下载时应该会立即弹出此错误。

【讨论】:

  • 其实进度条没有显示,那只是工作时的截图。
【解决方案2】:

您只需要将您的 URL 从 github 转换为原始文件。 像这样的:

https://raw.githubusercontent.com/MattMcManis/Glow/releases/download/v0.0.5.4-alpha/Glow.zip

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-04
    • 2021-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多