Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
I'm trying to write a c# code which can do FTPS(FTP with SSL) from a server whose 990 port is active (and 989 is inactive). I don't want to use any third party component. Please share some C# code example to implement the FTP with SSL. I'm trying with the below code. But getting "unable to connect remote server" error. I have checked the credentials and it was correct.

C#
public static void Download(string fileName)
        {
            FtpWebRequest reqFTP;
            FileStream outputStream = null;
            Stream ftpStream = null;
            FtpWebResponse response = null;
            //FtpWebRequest.RegisterPrefix("ftps://",System.Net.FtpWebRequest.Create(""));
           
            try
            {
                //filePath = <<The full path where the
                //file is to be created. the>>,
                //fileName = <<Name of the file to be createdNeed not
                //name on FTP server. name name()>>
                outputStream = new FileStream(Constants.TempFilePath +
                                        @"\" + fileName, FileMode.Create);

               
                reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftps://" +
                                        Constants.FtpServerIP + "/" + fileName));

                MessageLogger.LogMessage("FTPS Path created for each file: " + reqFTP, InformationType.Information);
                reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
                reqFTP.UseBinary = true;
                reqFTP.EnableSsl = true;
                reqFTP.Credentials = new NetworkCredential(Constants.FtpUserID, Constants.FtpPassword);
                response = (FtpWebResponse)reqFTP.GetResponse();
                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);
                }               
            }
Posted
Updated 9-May-11 1:44am
v2

references you could have found on google (like I did)

A reference to the different variations of the FTP protocol:

http://www.rebex.net/kb/secure-ftp.aspx[^]

A free third party library for ftp/ftps:

http://starksoftftps.codeplex.com/[^]

Google is free, and easy to use.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 9-May-11 13:49pm    
Good to know references, my 5.
--SA
Pinaki Bhattacharyya 10-May-11 3:08am    
@John
Hi John,
Thanks for your time.
do you have any kind of code sample? As per my requirement I can't use ant third party control.
FTP, FTP/SSL and SFTP are different. FTP is plain file transfer protocol, FTP/SSL stands for FTP over TLS/SSL, SFTP stands for File Transfer over SSH Channel protocol. You can see the differences at http://www.componentpro.com/sftp-scp-ftp-ftp-ssl-ftps-secure-ftp-comparison-id_185. Here file transfer example which uses Ultimate FTP library: http://www.componentpro.com/doc/ftp/Uploading-multiple-files-and-directories2.htm
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900