关于文件下载过程中网络断开连接的处理

coffee_sugar coffee_sugar 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
RT
我下载文件是这么写的:

Response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);
sio = RandomStream.AsStreamForWrite();
using (Stream stream = await Response.Content.ReadAsStreamAsync())
{
int read = 0;
byte[] responseBytes = new byte[1024];
do
{
read = await stream.ReadAsync(responseBytes, 0, responseBytes.Length);
DownloadBytes += read;
await sio.WriteAsync(responseBytes, 0, read);
Progress = DownloadBytes * 100 / TotalBytes.Value;
if (read == 0)
Complete();
} while (stream.CanRead && read != 0 && IsDownloading);
}

在文件下载中的时候如果突然断开网络连接的话,我发现这整个下载过程不会抛任何异常,而且Response的statecode也不会变,只是会在stream里的东西读完后,在read = await stream.ReadAsync(responseBytes, 0, responseBytes.Length);这句代码处突然跳出单步调试,啥反应都没有
不知道有没有人也是这么写文件下载的,对于这种断网问题怎么处理的呢?
或者还有别的下载文件的方式吗?
...全文
评论 3
收藏 已收藏
分享
返回顶部