簡體   English   中英

Stream.Read 返回 System.IO.IOException

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

我正在嘗試使用以下代碼下載大 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);
        }
    }
}

但下載幾秒后,程序返回System.IO.IOException: 'The decryption operation failed, see inner exception.' 並且內部異常是: Win32Exception: The specified data could not be decrypted
編輯:完整的例外是:

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

我無法重現這個問題

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

或這個

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

在 .NET Core 3.1 中,WebRequest 使用 HttpClient,因此這些代碼段之間沒有太大區別。

此代碼中唯一加密/解密數據的是 SSL 連接。 獲得解密錯誤意味着連接受到了某種影響。 也許服務器斷開連接導致不完整的包解密失敗? 或者連接中斷,部分數據丟失? 是否使用了 Fiddler 或其他一些調試代理?

更新

I changed to LAN and it worked所有 Wi-Fi 連接以一種或另一種方式失敗。 也許信號很弱,或者鄰居在同一個頻道上發出嘈雜的聲音,或者電信公司的廉價路由器出於惡意決定丟棄一些包裹。 咖啡館中過度工作的路由器可能沒有 CPU 處理所有連接的能力,因此它會在一段時間后開始丟棄低優先級的包

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM