【问题标题】:Error : "The request was aborted: The connection was closed unexpectedly." while using WebClient.DownloadFIleAsync错误:“请求中止:连接意外关闭。”使用 WebClient.DownloadFIleAsync 时
【发布时间】:2015-12-04 04:20:52
【问题描述】:

我在我的代码中使用 C#,Visual Studio 2015 社区版。

我想从私有Github存储库异步下载发布文件,并且已经有下载Uri:

https://github.com///releases/download/1.0.7.4/

如果我使用上面的 Uri,总是会出现这个错误:

请求中止:连接意外关闭。

奇怪的是,如果我从我的 repo 中尝试另一个 Uri,它会成功下载。

这是我的另一个 Uri:https://raw.githubusercontent.com///master/

这是我的代码

// Set up WebClient to download file
        webClient = new WebClient();
        webClient.Proxy = WebRequest.DefaultWebProxy;
        webClient.Proxy.Credentials = CredentialCache.DefaultCredentials;
        //webClient.UseDefaultCredentials = true;
        //webClient.Headers.Add(HttpRequestHeader.Authorization, string.Format("token {0}", applicationInfo.TokenAuthorization));
        webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(webClient_DownloadProgressChanged);
        webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(webClient_DownloadFileCompleted);

        // Download file
        try
        { webClient.DownloadFileAsync(updateInfo.Uri, this.temp7zFile); }
        catch { this.DialogResult = DialogResult.No; this.Close(); }

我已尝试添加(现在已备注)

webClient.UseDefaultCredentials = true;

webClient.Headers.Add(HttpRequestHeader.Authorization, string.Format("token {0}", applicationInfo.TokenAuthorization));

结果总是一样 = 请求被中止:连接意外关闭。

我真的不知道怎么了,我已经挣扎了3天..

注意:

- 这两个 Uri 都是正确的,如果我在 firefox 浏览器中运行它,它会自动下载。

- applicationInfo.TokenAuthorization : 是 Github 授权的令牌。

- updateInfo.Uri : 是 Uri 地址

- this.temp7zFile : 是目标下载文件

【问题讨论】:

    标签: c# github webclient


    【解决方案1】:

    我终于找到了差距。错误的 URL,必须处理重定向!

    基于Github Release,从私有仓库下载二进制或发布文件,使用资产的Url。

    Github 会从http://github-cloud.s3.amazonaws.com 发送重定向下载网址,其中包含一些参数。

    您只需在 WebClient.DownloadFileAsync 中使用该 URL,无需标头或授权等,该文件将永远快乐地到达您的本地驱动器..

    这都是因为私人回购。

    希望对大家有用..

    【讨论】:

      猜你喜欢
      • 2021-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-27
      • 2020-11-05
      • 1970-01-01
      • 2020-04-08
      相关资源
      最近更新 更多