开发者

Exception with WebClient.DownloadFileAsync

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 conn开发者_开发百科ection 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.


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?


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 @"..."

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜