C# – Error when trying to download a file using .net FtpWebRequest class

cftp

Error message is:

The server returned an address in response to the PASV command that is different than the address to which the FTP connection was made.

I get this message when I try to call GetResponse() method below…
Please help.

Here is my C# code :

FileStream outputStream = new FileStream(feedXmlPath + "\" +
"testXml", FileMode.Create);

        FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpURL + "/" + zipFileName);

        request.UseBinary = true;

        request.Credentials = new NetworkCredential(userName, password);

        request.KeepAlive = false;

        request.Method = WebRequestMethods.Ftp.DownloadFile;

      FtpWebResponse response = (FtpWebResponse)request.GetResponse();
      Stream ftpStream = response.GetResponseStream();

      long cl = response.ContentLength;
      int bufferSize = 2048;
      int readCount;
      byte[] buffer = new byte[bufferSize];

      readCount = ftpStream.Read(buffer, 0, bufferSize);


        while (readCount > 0)
      {
          outputStream.Write(buffer, 0, readCount);
          readCount = ftpStream.Read(buffer, 0, bufferSize);
      }

      ftpStream.Close();
      outputStream.Close();
      response.Close();

Best Answer

The use of FTPWebRequest isn't permitted for security reasons if you're using NAT. Check out this post on Connect.

This post on MSDN might be helpful too.