1

I am using this code to connect my server and downloading the files. It is working fine.

public void downloadFile(object args)
{
    writeStream = null;
    response = null;
    reader = null;
    int dataLength = 0;

    try
    {
        Array argArray = new object[3];
        argArray = (Array)args;
        ProgressBar progressBar1 = (ProgressBar)argArray.GetValue(0);
        Label lbProgress = (Label)argArray.GetValue(1);

        FTPInfo ftpInfo = (FTPInfo)argArray.GetValue(2);
        string ipAddress = ftpInfo.IpAddress;

        string path = ftpInfo.Path;
        string fileName = ftpInfo.FileName;

        path = Regex.Replace(path, "_.", "_e");
        fileName = Regex.Replace(fileName, "_.", "_e");

        string uri = null;
        if (path.Equals(""))
        {
            uri = ipAddress + fileName;
        }
        else
        {
            uri = ipAddress + path + "/" + fileName;
        }
        string[] temp = ipAddress.Split('/');
        string ip = "mchmultimedia.com";
        string userName = ftpInfo.UserName;
        string password = ftpInfo.Password;
        downloadedData = new byte[0];
        ftp = new FTPClass(path);
        ftp.FtpServer = ip;
        ftp.FtpUsername = userName;
        ftp.FtpPassword = password;
        ftp.FtpLogin();
        dataLength = (int)ftp.GetFileSize(fileName);
        Logger.LogDebugMessage("DataLength :" + dataLength.ToString());
        ftp.CloseConnection();
        FtpWebRequest request = FtpWebRequest.Create(uri) as FtpWebRequest;
        request.Method = WebRequestMethods.Ftp.DownloadFile;
        request.Credentials = new NetworkCredential(userName, password);
        request.UsePassive = true;
        request.UseBinary = true;
        request.KeepAlive = false;

        //Set up progress bar
        UpdateProgressBarValue(progressBar1, 0);
        SetProgressBarMaxValue(progressBar1, dataLength);

        response = request.GetResponse() as FtpWebResponse;
        reader = response.GetResponseStream();

        if (!Directory.Exists(GlobalClass.ZIPPED_FOLDER))
            Directory.CreateDirectory(GlobalClass.ZIPPED_FOLDER);
        writeStream = new FileStream(GlobalClass.ZIPPED_FOLDER + "\\" + fileName, FileMode.Create);
        int Length = 2048;
        Byte[] buffer = new Byte[Length];
        int bytesRead = 0;
        Logger.LogDebugMessage("Before while :" + dataLength.ToString());
        while (true)
        {
            Application.DoEvents(); 

            bytesRead = reader.Read(buffer, 0, buffer.Length);
            if (bytesRead == 0)
            {
                try
                {
                    try
                    {
                        reader.Close();
                    }
                    catch (Exception ex)
                    {
                        Logger.LogErrorMessage("reader close if bytesRead ==00", ex);

                    }

                    try
                    {
                        response.Close();
                    }
                    catch (Exception ex)
                    {
                        Logger.LogErrorMessage("response close if  bytesRead ==00", ex);
                    }

                    try
                    {
                        writeStream.Close();
                    }
                    catch (Exception ex)
                    {
                        Logger.LogErrorMessage("writeStream close if  bytesRead ==00", ex);
                    }

                }
                catch (Exception ex)
                {

                }
                UpdateProgressBarValue(progressBar1, progressBar1.Maximum);
                Application.DoEvents();
                break;
            }
            else
            {
                writeStream.Write(buffer, 0, bytesRead);

                if (progressBar1.Value + bytesRead <= progressBar1.Maximum)
                {
                    totalBytesRead = progressBar1.Value + bytesRead;
                    int percentage = (int)((double)totalBytesRead / dataLength * 100);
                    UpdateProgressBarValue(progressBar1, totalBytesRead);

                    SetText(lbProgress, "File download " + percentage.ToString() + " % completed");
                    if (percentage == 100)
                    {
                        if (totalBytesRead == dataLength)
                        {
                            try
                            {
                                try
                                {
                                    reader.Close();
                                }
                                catch (Exception ex)
                                {
                                    Logger.LogErrorMessage("reader close if percentage==100", ex);
                                }

                                try
                                {
                                    response.Close();
                                }
                                catch (Exception ex)
                                {
                                    Logger.LogErrorMessage("response close if percentage==100", ex);
                                }

                                try
                                {
                                    writeStream.Close();
                                }
                                catch (Exception ex)
                                {
                                    Logger.LogErrorMessage("writeStream close if percentage==100", ex);
                                }

                            }
                            catch (Exception ex)
                            {

                            }
                            SetText(lbProgress, "File download successfully completed,Please wait...");
                            unzipFile(fileName);
                        }

                    }

                    RefreshProgressBar(progressBar1);
                    Application.DoEvents();
                }

            }
        }
    }
    catch (System.Threading.ThreadAbortException ex)
    {
        if (thread != null)
        {
            thread.Abort();
        }
        Logger.LogErrorMessage("ThreadAbortException", ex);

    }
    catch (Exception ex)
    {
        Logger.LogErrorMessage("Exception there was an error connecting", ex);
        Logger.ReportBug("There was an error connecting to the FTP Server.", ex);
    }
    finally
    {
        try
        {
            try
            {
                reader.Close();
            }
            catch (Exception ex)
            {
                Logger.LogErrorMessage("read finally", ex);
            }

            try
            {
                response.Close();
            }
            catch (Exception ex)
            {
                Logger.LogErrorMessage("resonse finally", ex);
            }

            try
            {
                writeStream.Close();
            }
            catch (Exception ex)
            {
                Logger.LogErrorMessage("writeStream finally", ex);
            }
        }
        catch (Exception ex)
        {

        }
    }
}

But client needs it to be secure FTP. So I tried by setting

request.EnableSsl = true;

as specified in Differences between SFTP and "FTP over SSH"

And it throws:

The remote server returned an error: (500) Syntax error, command unrecognized.

Community
  • 1
  • 1
Vivekh
  • 4,141
  • 11
  • 57
  • 102
  • So does the client require you to use Secure FTP (as per your text and core you use) or SFTP (tag you have used)? Did you really understand the difference, when reading [the question you linked to](http://stackoverflow.com/questions/440463/differences-between-sftp-and-ftp-over-ssh)? The 500 error would indicate that the server does not understand `AUTH TLS` command, so that it does not support FTP over TLS. Maybe you are supposed to use SFTP? Is there a port specified in `ftpInfo.IpAddress`? – Martin Prikryl Jan 29 '15 at 12:39
  • I am little bit confused. I have used SSH.NET Nuget package and it worked well but i just came across this link that says you can enable security by just enabling SSl So i have tried that. – Vivekh Jan 29 '15 at 12:50

1 Answers1

1

You are obviously required to use the SFTP protocol.

The FtpWebRequest class does not support the SFTP protocol. There's no support at all for SFTP in standard .NET libraries. See SFTP Libraries for .NET.

You current code is trying to connect with the FTP over TLS protocol to the FTP server. That is an another way of securing "file transfer" session. But this particular FTP server does not support it (hence the "500 Syntax error, command unrecognized" error).

So you have to rewrite your code to use the SFTP protocol.

That's unfortunately a completely different code.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
  • Hey i would like to ask if there is any affect on the time taken to download the file from normal ftp (built in .NET) and using SSH.NET or any other 3rd party SFTP services – Vivekh Jan 29 '15 at 15:11
  • SFTP can be slower. 1) Due to the encryption, if either side of the transfer has limited CPU power. 2) SFTP (and underlying SSH) protocols are packet oriented. If their implementation is not ideal (again on either side), capacity of a connection to/from the server may not be utilized to its maximum. Neither of this is an issue with FTP protocol. FTP protocol on the contrary is less efficient that SFTP, when dealing with lots of small files. – Martin Prikryl Jan 29 '15 at 15:50