1
votes

I keep get this error while trying to download a file using WebClient.DownloadFileAsync

System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive. ---> System.IO.IOException: Unable to read data from the transport connection: An established connection was aborted by the software in your host machine. ---> System.Net.Sockets.SocketException: An established connection was aborted by the software in your host machine
at System.Net.Sockets.Socket.EndReceive(IAsyncResult asyncResult) at System.Net.Sockets.NetworkStream.EndRead(IAsyncResult asyncResult)
--- End of inner exception stack trace ---
at System.Net.Sockets.NetworkStream.EndRead(IAsyncResult asyncResult)
at System.Net.PooledStream.EndRead(IAsyncResult asyncResult) at System.Net.Connection.ReadCallback(IAsyncResult asyncResult) --- End of inner exception stack trace --- at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult) at System.Net.WebClient.GetWebResponse(WebRequest request, IAsyncResult result) at System.Net.WebClient.DownloadBitsResponseCallback(IAsyncResult result)

The code:

try
{
    client.DownloadFileAsync(URL, location);
    client.Dispose();
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}
1
press ctrl+c on message box to copy the error message and paste it here instead of uploading it's picture. - Hamid Pourjam
can you give us the url and location parameter? - Hamid Pourjam
Well i'm not sure how to do that, in java i use synchronized(OBJECT_LOCK).... to avoid those kind of errors but in c# i'm still learning so could you be more clear if you don't mind And okay the link is the code pastebin.com/mPNsyrXC - user3027239
try testing your code with known file url. maybe the url is incorrect - Hamid Pourjam
That actually worked for me, thanks alot where i can give rep if that possible :D - user3027239

1 Answers

0
votes

I think the problem is in the URL you supplied to the DownloadFileAsync method. try double checking the URL, test it with a known URL that exists.