我有异步下载问题。有时它崩溃了。该方法在尝试中,但无济于事。
如何防止文件下载停止整个应用程序?
我有跟踪记录,但无济于事,我知道错误在哪里,但是为什么应用程序崩溃了?
Inner exceptions:
An error occurred while sending the request.
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at WhiteListAPI.FileTool.<DownloadAsync>d__0.MoveNext()
— End of stack trace from previous location where exception was thrown —
at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<>c.<ThrowAsync>b__6_1(Object state)
at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
at System.Threading.ThreadPoolWorkQueue.Dispatch()
at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()
The underlying connection was closed: An unexpected error occurred on a receive.
at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
at System.Net.Http.HttpClientHandler.GetResponseCallback(IAsyncResult ar)
Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.
at System.Net.Security._SslStream.EndRead(IAsyncResult asyncResult)
at System.Net.TlsStream.EndRead(IAsyncResult asyncResult)
at System.Net.PooledStream.EndRead(IAsyncResult asyncResult)
at System.Net.Connection.ReadCallback(IAsyncResult asyncResult)
An existing connection was forcibly closed by the remote host
at System.Net.Sockets.Socket.EndReceive(IAsyncResult asyncResult)
at System.Net.Sockets.NetworkStream.EndRead(IAsyncResult asyncResult)
public async void DownloadAsync(string fileAdress, string fileDestination)
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(fileAdress);
client.Timeout = TimeSpan.FromMinutes(10);
var request = new HttpRequestMessage(HttpMethod.Post, fileAdress);
var sendTask = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);
var response = sendTask.EnsureSuccessStatusCode();
var httpStream = await response.Content.ReadAsStreamAsync();
using (var fileStream = System.IO.File.Create(fileDestination))
using (var reader = new StreamReader(httpStream))
{
httpStream.CopyTo(fileStream);
fileStream.Flush();
}
}
}
public FileWhiteList Download(string fileAdress, string fileDestination)
{
var status = new FileWhiteList();
try
{
DownloadAsync(fileAdress, fileDestination);
}
catch (Exception ex)
{
status.report = ex.ToString();
status.ok = false;
}
if (!string.IsNullOrEmpty(status.report))
throw new Exception(status.report);
status.report = "The file has been downloaded correctly " + fileAdress;
status.ok = true;
return status;
}