问题描述
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,4096);
while (bytesRead > 0)
{
fileStream.Write(buffer,bytesRead);
DateTime NowTime = DateTime.UtcNow;
if ((NowTime - startTime).TotalMinutes > 5)
{
throw new ApplicationException("Download timed out");
}
bytesRead = responseStream.Read(buffer,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连接。获得解密错误意味着连接受到某种影响。也许 server 断开了连接,导致软件包不完整,解密失败?还是连接中断并且丢失了一些数据?可能使用了Fiddler或其他调试代理吗?
更新
I changed to LAN and it worked
所有Wi-Fi连接都以一种或另一种方式失败。也许信号很弱,或者有吵闹的邻居在同一信道上发出信号,或者这家电信公司的廉价路由器决定放弃某些软件包。咖啡馆中工作过度的路由器可能没有CPU能力来处理所有连接,因此一段时间后它将开始丢弃低优先级的软件包