The main connection was closed: an unexpected error occurred while receiving

When I try to execute my program on my win2k8 machine, it works fine, but when I win 2k3 it gives me this error, that the error message

here is the code that "generates an error

WebClient wc = new WebClient(); wc.DownloadFile("ftp://ftp.website.com/sample.zip"); 

there is a strange part. if I completely disable the firewall on the server. the error goes away. but it’s still an error if I add the program to the list of exceptions and turn on the firewall.

search the Internet for several days, could not find a solution.

+4
source share
3 answers

You should try using passive mode for FTP. The WebClient class does not allow this, but FtpWebRequest does.

 FtpWebRequest request = WebRequest.Create("ftp://ftp.website.com/sample.zip") as FtpWebRequest; request.UsePassive = true; FtpWebResponse response = request.GetResponse() as FtpWebResponse; Stream ftpStream = response.GetResponse(); int bufferSize = 8192; byte[] buffer = new byte[bufferSize]; using (FileStream fileStream = new FileStream("localfile.zip", FileMode.Create, FileAccess.Write)) { int nBytes; while((nBytes = ftpStream.Read(buffer, 0, bufferSize) > 0) { fileStream.Write(buffer, 0, nBytes); } } 
+6
source

Please post the full exception, including any InnerException:

 try { WebClient wc = new WebClient(); wc.DownloadFile("ftp://ftp.website.com/sample.zip"); } catch (Exception ex) { Console.WriteLine(ex.ToString()); // Or Debug.Trace, or whatever throw; // As if the catch were not present } 
+1
source

I had a similar problem (no ftp) and another solution

The production server environment was so secure that the server could not reach the URL.

A quick test is to use the browser in the field and see if you can go to the URL.

Hope this helps someone.

+1
source

All Articles