Upload file via FTP TLS

Asked

Viewed 177 times

2

I am trying to upload files via FTP (TLS), but I am not successful because it always returns an error when my code runs "Connect()". I have tried using Ftpwebrequest , Ftpclient, Tamir.Sharpssh and Sftpclient. Any of them during the "Connect()" instruction I get a connection failure message, follow detail:

a) Connect with SFTP classes Example:

    private void UploadSFTPFile(string host, string username, string 
           password, string sourcefile, string destinationpath, string port)
    {
        int port2 = Convert.ToInt32(port);

        try
        {
            using (SftpClient client = new SftpClient(host, port2, username, password))
            {
                client.Connect();  //o erro ocorre aqui!!
                client.ChangeDirectory(destinationpath);

                using (FileStream fs = new FileStream(sourcefile, FileMode.Open))
                {
                    try
                    {
                        client.BufferSize = 4 * 1024;
                        client.UploadFile(fs, Path.GetFileName(sourcefile));
                        Global.retorno = "Envio via SFTP " + host + " efetuado com sucesso.";
                    }

                    catch (Exception ex)
                    {
                        Global.retorno = "Erro no envio do arquivo via SFTP (1)" + host + ". Detalhe: " + ex;
                    }
                }
            }
        }
        catch (Exception ex)
        {
            Global.retorno = "Erro na conexão via SFTP (2)" + host + ". Detalhe: " + ex;
        }
    }

Error message: "A connection attempt failed because the connected component did not respond correctly after a period of time or the established connection failed because the connected host did not respond..."

IMPORTANT: I also tried to use the class/library "Winscp", for this case I had the following return message: "Connection failed. Authentication failed. Connection failed. TLS required".

The strange thing is that with Filezila I connect without problems informing the host, username and passaword. I do not need to inform key or certificate and connect normally. So, I do not have clarity of what is occurring.

Should I or should I not use a key/certificate to connect? Even Filizila does not need this...

Obs.Framework 4.5.2

Thanks in advance.

2 answers

2

Yes, but it is not a simple FTP, it asks for a public key (TLS) which I was not aware of, I just found the solution. I basically used Winscp (https://winscp.net/eng/docs/library). What I believe is relevant to those who go through the same problem (because I suffered to find out), is that within Filezila it is possible to discover the public key and use it in the code as below (tkey):

    private void UploadFTPTLSFile(string host, string username, string password, string sourcefile, string destinationpath, string port, string tkey)
    {
        //https://winscp.net/eng/docs/library

        int port2 = Convert.ToInt32(port);
        destinationpath = destinationpath + "\\" + arquivoUpFtpChk;

        try
        {
            SessionOptions sessionOptions = new SessionOptions
            {
                Protocol = Protocol.Ftp,
                HostName = host,
                UserName = username,
                Password = password,
                FtpSecure = FtpSecure.Implicit,
                TlsHostCertificateFingerprint = tkey //aqui vai a chave pública que pode ser descoberta a qual o Filezila utiliza...
            };

            using (WinSCP.Session session = new WinSCP.Session())
            {
                // Connect
                session.Open(sessionOptions);

                // Upload files
                TransferOptions transferOptions = new TransferOptions();
                transferOptions.TransferMode = TransferMode.Binary;

                TransferOperationResult transferResult;
                transferResult =
                    session.PutFiles(sourcefile, destinationpath, false, transferOptions);

                // Throw on any error
                transferResult.Check();

                // Print results
                foreach (TransferEventArgs transfer in transferResult.Transfers)
                {
                    Global.retorno = ("Upload efetuado com sucesso:" + transfer.FileName);
                }
            }
        }

        catch (Exception ex)
        {
            Global.retorno = ("Erro: " + ex);
        }
    }
  • Yes, no doubt it will be relevant Cleber! Then you can accept your own answer to your question.

1

Have you tried using the Ftpwebrequest thus?

FtpWebRequest requestFTP = (FtpWebRequest)WebRequest.Create(urlFTP);
requestFTP.Credentials = new NetworkCredential(usuario, senha);
requestFTP.EnableSsl = true;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.