System.Net.WebException:无法从传输连接中读取数据:连接已关闭。 [英] System.Net.WebException: Unable to read data from the transport connection: The connection was closed.

查看:974
本文介绍了System.Net.WebException:无法从传输连接中读取数据:连接已关闭。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Sometimes the following error occurs by downloading a (large) file:


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

System.Net.WebException: An exception occurred during a WebClient request. ---> System.IO.IOException: Unable to read data from the transport connection: The connection was closed. at System.Net.ConnectStream.EndRead(IAsyncResult asyncResult) at System.Net.WebClient.DownloadBitsReadCallbackState(DownloadBitsState state, IAsyncResult result) --- End of inner exception stack trace ---

对我来说,这听起来好像服务器超时了,但我不确定。我希望你们能向我解释这个问题。

For me that sounds like the server times out but I am not sure about that. I hope you guys can explain me that issue. And mabe offer a fix for that.

下载逻辑如下所示:

            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);


推荐答案

经过一番研究并在@TetsuyaYamamoto的帮助下,

After some research and with the help of @TetsuyaYamamoto I found a solution that works for me so far.

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

I created a class derived from WebClient and I modify it's 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

It basically does what @TetsuyaYamamoto recommended in his comment. I am just changing the Procolversion for the WebRequest to HTTP 1.0

这篇关于System.Net.WebException:无法从传输连接中读取数据:连接已关闭。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆