首页 文章

System.Net.WebException:无法从传输连接读取数据:连接已关闭 .

提问于
浏览
1

有时通过下载(大)文件会发生以下错误:

System.Net.WebException:WebClient请求期间发生异常 . ---> System.IO.IOException:无法从传输连接读取数据:连接已关闭 . 在System.Net.WebClient.DownloadBitsReadCallbackState(DownloadBitsState状态,IAsyncResult结果)的System.Net.ConnectStream.EndRead(IAsyncResult asyncResult)---内部异常堆栈跟踪结束---

对我来说,这听起来像服务器超时,但我不确定 . 我希望你们能解释我这个问题 . mabe为此提供了解决方案 .

下载逻辑看起来像这样:

WebClient client = new WebClient();
            client.DownloadFileCompleted += (sender, e) =>
            {
                if (e.Error != null)
                {
                    BridgeManager.logNow("DownloadingError:\n" + e.Error.ToString());
                }

                if (e.Cancelled)
                {
                    BridgeManager.logNow("Game file download canceled!");
                }
                else
                {
                    BridgeManager.logNow("Game files downloaded!");
                    success = true;
                    downloadFinished.Set();
                }
            };




            client.DownloadFileAsync(downloadGameAddr, file);

1 回答

  • 0

    经过一些研究并在@TetsuyaYamamoto的帮助下,我找到了一个迄今为止对我有用的解决方案 .

    我创建了一个派生自WebClient的类,我修改了它的WebRequest:

    public class MyWebClient : WebClient
    {
        protected override WebRequest GetWebRequest(Uri uri)
        {
            HttpWebRequest w = (HttpWebRequest)base.GetWebRequest(uri);
            w.ProtocolVersion = Version.Parse("1.0");
            return (WebRequest)w;
        }
    }
    

    它基本上是@TetsuyaYamamoto在评论中推荐的 . 我只是将WebRequest的Procolversion更改为HTTP 1.0

相关问题