Reputation: 784
I am not able to connect to a specific FTP using C# FtpWebRequest
.
With TotalCommander or FileZilla I am able to connect.
Information about FTP say:
FTPS – implicit SSL/TLS
Port 990
Range of passive ports: 60000–60100
Another FTP works well.
var remoteFile = "ftp://ADDRESS:990/FILE.csv";
var localFile = Server.MapPath("~/tmp/file.csv");
int bufferSize = 2048;
if (!Directory.Exists(Path.GetDirectoryName(localFile)))
Directory.CreateDirectory(Path.GetDirectoryName(localFile));
/* Create an FTP Request */
FtpWebRequest ftpRequest = (FtpWebRequest)FtpWebRequest.Create(remoteFile);
/* Log in to the FTP Server with the User Name and Password Provided */
ftpRequest.Credentials = new NetworkCredential("USER", "PWD");
/* When in doubt, use these options */
ftpRequest.UseBinary = true;
ftpRequest.UsePassive = true;
ftpRequest.KeepAlive = true;
ftpRequest.EnableSsl = true;
// Always returns true
ServicePointManager.ServerCertificateValidationCallback = OnValidateCertificate;
/* Specify the Type of FTP Request */
ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
/* Establish Return Communication with the FTP Server */
FtpWebResponse ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
/* Get the FTP Server's Response Stream */
Stream ftpStream = ftpResponse.GetResponseStream();
/* Open a File Stream to Write the Downloaded File */
FileStream localFileStream = new FileStream(localFile, FileMode.Create);
/* Buffer for the Downloaded Data */
byte[] byteBuffer = new byte[bufferSize];
int bytesRead = ftpStream.Read(byteBuffer, 0, bufferSize);
/* Download the File by Writing the Buffered Data Until the Transfer is Complete */
try
{
while (bytesRead > 0)
{
localFileStream.Write(byteBuffer, 0, bytesRead);
bytesRead = ftpStream.Read(byteBuffer, 0, bufferSize);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
/* Resource Cleanup */
localFileStream.Close();
ftpStream.Close();
ftpResponse.Close();
ftpRequest = null;
In my opinion the error is caused by passive port range 60000–60100. But I don't know how to set it up.
Upvotes: 1
Views: 2944
Reputation: 1
you will never to able to connect to implicit FTPS from a Microsoft framework. Its an old third-party framework which isn't secure. Change the client to explicit FTPS.
Upvotes: -1
Reputation: 202262
The FTP passive port range is a server-side configuration.
You do not set the passive port range on client side – FileZilla nor Total Commander do not have such configuration option either. FTP client uses the port chosen by the server.
Your actual problem is rather that .NET/FtpWebRequest
does not support implicit TLS/SSL:
Does .NET FtpWebRequest Support both Implicit (FTPS) and explicit (FTPES)?
Upvotes: 2