I was trying to use SSL in ftpwebrequest like below
FileStream outputStream = new FileStream(fileName, FileMode.Append);
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpserverIp + "/" + file));
reqFTP.EnableSsl = true;
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
reqFTP.UseBinary = true;
reqFTP.KeepAlive = false;
reqFTP.Timeout = -1;
reqFTP.UsePassive = true;
reqFTP.Credentials = new NetworkCredential("sh", "SE");
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
Stream ftpStream = response.GetResponseStream();
long cl = response.ContentLength;
// reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
int bufferSize = 4096;
int readCount;
byte[] buffer = new byte[bufferSize];
readCount = ftpStream.Read(buffer, 0, bufferSize);
Console.WriteLine("Connected: Downloading File");
while (readCount > 0)
{
outputStream.Write(buffer, 0, readCount);
readCount = ftpStream.Read(buffer, 0, bufferSize);
//Console.WriteLine(readCount.ToString());
}
ftpStream.Close();
outputStream.Close();
response.Close();
I got this below exception:
The remote certificate is invalid according to the validation procedure.
Based on my google remove the private key part in the certificate and start processing now it throws
431 could not initialize ssl connection
I tried googling but no result so far,any idea guys.
Am trying to connect FileZilla.
The answer is simple that FtpWebRequest does not even support FTPS good enough.
Quoted from http://ftps.codeplex.com/
Microsoft only starts to support FTPS on server side when they provide FTP 7.5 for Windows Server 2008 and above. Up to now, they don't even support FTPS in Internet Explorer nor Windows Explorer. No wonder .NET Framework BCL lacks FTPS support.
In this segment of code:
new Uri("ftp://" + ftpserverIp + "/" + file)
you're attempting to connect to a non-SSL ftp server, which is denoted with "ftp". Changing "ftp://" to "ftps://" should help (assuming the server supports SSL.)The step you're missing is establishing a policy for certificate validation.
This is done with the following code:
and then setting the above method to be hit when you get a certificate check:
The given example works in .NET 3.5 meaning at the time of the currently accepted answer, the statement that this setup was impossible was already wrong.