【发布时间】:2017-07-19 21:37:06
【问题描述】:
下载(大)文件有时会出现以下错误:
System.Net.WebException:在 WebClient 请求期间发生异常。 ---> System.IO.IOException:无法从传输连接读取数据:连接已关闭。 在 System.Net.ConnectStream.EndRead(IAsyncResult asyncResult) 在 System.Net.WebClient.DownloadBitsReadCallbackState(DownloadBitsState 状态,IAsyncResult 结果) --- 内部异常堆栈跟踪结束 ---
对我来说,这听起来像是服务器超时,但我不确定。我希望你们能解释我这个问题。并且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);
【问题讨论】:
-
您是否以不被垃圾回收的方式维护“客户端”对象?
-
@Juan 我不知道。但是客户端在任务中使用,我不确定这是否重要..
-
先尝试使用
DownloadFile而不是DownloadFileAsync,看看它是否正常工作。如果错误仍然存在,似乎服务器响应了分块数据以供下载,其中EndRead无法识别 EOF 条件(将标头响应设置为 HTTP/1.0 作为解决方法),或使用<httpRuntime maxRequestLength="[max request size in bytes]" />修改最大页面请求大小。跨度> -
@TetsuyaYamamoto 我找到了如何使用“Webclient.Headers.Set”修改标题但我不知道我必须输入什么值?
标签: c# download timeout webclient