4

When I try my program on my win2k8 machine it runs fine but on win 2k3 it gives me this error that error message

here's the code that' generating the error

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

there's the weird part. if i disable the firewall on the server completely. the error goes away. but it still errors if i add the program to the exception list and turn on the firewall.

have search the web for days, couldn't find any solution.

3 Answers3

6

You should try using passive mode for FTP. The WebClient class doesn't allow that, 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);
    }
}
Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758
  • how do i use FtpWebRequest to download a file and save it to my local drive? I examples i found only show how to display server response. –  May 17 '09 at 21:52
  • it was a mistake, I meant nBytes... it's fixed now – Thomas Levesque May 18 '09 at 06:46
  • See Stream.CopyTo() in .NET 4.0 http://msdn.microsoft.com/en-us/library/system.io.stream.copyto.aspx – Hans Mar 01 '13 at 18:41
  • I am also getting this error even with FtpWebRequest in passive mode. – Hans Mar 01 '13 at 18:42
  • This fixed it for me. I was already using FtpWebRequest, not WebClient, but I was getting the exception at the very *end* of the Stream.CopyTo() operation, when the file I was downloading was actually complete and intact. Rather than just coding to swallow the exception, this passive mode fix eliminated it completely. – Jordan Rieger Feb 14 '18 at 17:17
1

I had a similar issue (no ftp) and a different solution

The production server environment had been made so secure that the server could not reach the URL.

Quick test is to use a browser on the box and see if you can navigate to the url.

Hope this helps someone.

Tim Jarvis
  • 667
  • 4
  • 9
  • 17
1

Please post the complete 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
}
John Saunders
  • 160,644
  • 26
  • 247
  • 397