views:

90

answers:

2

I am trying to download a zip file on my server by doing

WebClient.DownloadFileAsync(new Uri(DownloadLink),
                            Path.GetFileName(DownloadLink));

I have events linked to download completed and progress changed and when the download gets to 99%, it stops. So I give it a minute and then it gives me an exception:

System.Net.WebException: An exception occurred during a WebClient request. ---> System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. ---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host
   at System.Net.Sockets.NetworkStream.EndRead(IAsyncResult asyncResult)
   --- End of inner exception stack trace ---
   at System.Net.ConnectStream.EndRead(IAsyncResult asyncResult)
   at System.Net.WebClient.DownloadBitsReadCallbackState(DownloadBitsState state, IAsyncResult result)
   --- End of inner exception stack trace ---

I am stumped. I can download the file fine on my browser.

A: 

I don't have a direct answer to your question, but there are a few things I'd try in order to see if I could identify the problem.

First, get rid of the asynchronous call and just call DownloadFile. Does the error still occur?

Call DownloadData to get the data in a byte array, and then save the file.

If either or both of the above work, then I'd say there's something wrong with your DownloadDataCompleted event handler, although why that'd throw a WebException is beyond me.

Oh, one other thing: are you sure that Path.GetFileName(DownloadLink) is returning a valid file name, and that you have the necessary permissions to write the file in the current directory?

Jim Mischel
Same issues. And yes, it is a valid name. If I view the file, it is completely downloaded. Also, my code doesn't seem to be the problem.. I had some friends try it and the downloading worked for them. |:
Eaton
Two questions: 1) Calling DownloadData to get the file in a byte array also throws an exception? 2) Your friends tried running the same code you're running, and it works?
Jim Mischel
Yeah, and I am on another network now and it is working fine.
Eaton
A: 

You will get a WebException if DownloadLink returns an error like 404. Put a breakpoint in and check the actual path going into the method.

Edit: If DownloadLink is a url string check if you are escaping '/'. Try @"..."

karl.r
Not a 404, it would be a different error. Like I said, it technically downloads 99% of the file.
Eaton
This probably means that the server is rudely closing the connection on the client. To verify, you can either get a wireshark trace, or you can get a system.net trace log - see http://ferozedaud.blogspot.com/2009/08/tracing-with-systemnet.html
feroze
Can you get the tracelog as I suggested? That will show what is happening as far as the client is concerned.
feroze
Ok, I will try that soon and post my results.
Eaton