Error while trying to download file using .net class FtpWebRequest
Error message:
The server returned an address in response to the PASV command that is different from the address to which the FTP connection was made.
I get this message when I try to call the 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();
a source to share
Try toggling the passive value to see what works:
request.UsePassive = false;
It might depend on the firewall between the machines (client and server).
I noticed that if I go through our firewall I need it to stay at True, otherwise it will return an Exception:
The remote server returned an error: (500) Syntax error, command unrecognized.
However, if I am behind a firewall (for example, two machines directly connected to each other in the datacenter), I need to set it to False, otherwise it will return an Exception:
The server returned an address in response to the PASV command, which is different from the address to which the FTP connection was made.
a source to share