简体   繁体   English

Stream.Read 返回 System.IO.IOException

[英]Stream.Read return System.IO.IOException

I'm trying to download a large zip (560MB) with the following code:我正在尝试使用以下代码下载大 zip (560MB):

DateTime startTime = DateTime.UtcNow;
WebRequest request = WebRequest.Create("https://report-demo.eyeq.tech/download/Downloads.zip");
WebResponse response = request.GetResponse();
using (Stream responseStream = response.GetResponseStream())
{
    using (Stream fileStream = new FileStream("Downloads.zip", FileMode.Open, FileAccess.ReadWrite))
    {
        byte[] buffer = new byte[4096];
        int bytesRead = responseStream.Read(buffer, 0, 4096);
        while (bytesRead > 0)
        {
            fileStream.Write(buffer, 0, bytesRead);
            DateTime nowTime = DateTime.UtcNow;
            if ((nowTime - startTime).TotalMinutes > 5)
            {
                throw new ApplicationException("Download timed out");
            }
            bytesRead = responseStream.Read(buffer, 0, 4096);
        }
    }
}

But after a few second of downloading, the program returned System.IO.IOException: 'The decryption operation failed, see inner exception.'但下载几秒后,程序返回System.IO.IOException: 'The decryption operation failed, see inner exception.' And the inner Exception is: Win32Exception: The specified data could not be decrypted并且内部异常是: Win32Exception: The specified data could not be decrypted
EDIT: The full exception is:编辑:完整的例外是:

Exception thrown: 'System.IO.IOException' in System.dll
System.IO.IOException: The decryption operation failed, see inner exception. ---> System.ComponentModel.Win32Exception: The specified data could not be decrypted
   --- End of inner exception stack trace ---
   at System.Net.ConnectStream.Read(Byte[] buffer, Int32 offset, Int32 size)
   at WpfApp1.MainWindow.<Download>d__1.MoveNext() in D:\vinh.ngo\Project\WpfApp1\MainWindow.xaml.cs:line 53

I can't reproduce the problem with this我无法重现这个问题

var client=new System.Net.Http.HttpClient();
var url="https://report-demo.eyeq.tech/download/Downloads.zip";
using var stream=await client.GetStreamAsync(url);
using var fi=File.Create(@"c:\somepath.zip");
//Set up a 10" timeout
var cts=new CancellationTokenSource(TimeSpan.FromSeconds(10));
//Copy the data directly
await stream.CopyToAsync(fi,cts.Token);

or this或这个

var req=System.Net.WebRequest.Create(url);
using var response = req.GetResponse();
using var stream = response.GetResponseStream();
using var fi=File.Create(@"c:\somepath.zip");
//Set up the timeout
var cts=new CancellationTokenSource(TimeSpan.FromSeconds(10));
//Store the data
await stream.CopyToAsync(fi,cts.Token);
Console.WriteLine("Finished");

In .NET Core 3.1 WebRequest uses HttpClient so there's not a huge difference between those snippets.在 .NET Core 3.1 中,WebRequest 使用 HttpClient,因此这些代码段之间没有太大区别。

The only thing that encrypts/decrypts data in this code is the SSL connection.此代码中唯一加密/解密数据的是 SSL 连接。 To get a decryption error means the connection was somehow affected.获得解密错误意味着连接受到了某种影响。 Perhaps the server dropped the connection resulting in incomplete packages that failed decryption?也许服务器断开连接导致不完整的包解密失败? Or the connection was interrupted and some data was lost?或者连接中断,部分数据丢失? Is Fiddler or some other debugging proxy used perhaps?是否使用了 Fiddler 或其他一些调试代理?

Update更新

I changed to LAN and it worked all Wi-Fi connections fail one way or another. I changed to LAN and it worked所有 Wi-Fi 连接以一种或另一种方式失败。 Perhaps the signal is weak, or noisy neighbors emit on the same channel, or the telco's cheap router decided to drop some packages out of spite.也许信号很弱,或者邻居在同一个频道上发出嘈杂的声音,或者电信公司的廉价路由器出于恶意决定丢弃一些包裹。 An overworked router in a cafe may not have the CPU power to handle all connections so it will start dropping low priority packages after a while咖啡馆中过度工作的路由器可能没有 CPU 处理所有连接的能力,因此它会在一段时间后开始丢弃低优先级的包

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM