Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The underlying connection was closed. The server committed a protocol violation

I am trying to get the directory list of a FTPS FileZilla server using the code below :

ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + "/" + directory);
ftpRequest.EnableSsl = true;

ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateCertificate);
ftpRequest.Credentials = new NetworkCredential(user, pass);
ftpRequest.UseBinary = true;
ftpRequest.UsePassive = true;
ftpRequest.KeepAlive = true;
ftpRequest.Method = WebRequestMethods.Ftp.ListDirectory;
ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();

I got an exception when FtpWebResponse)ftpRequest.GetResponse() is executed :

the underlying connection was closed. The server committed a protocol violation.

When I switch to normal FTP connection. Everything works correctly.

Did I miss something to establish this FTPS connection ? thanks for help

like image 450
MadNeox Avatar asked Oct 09 '14 12:10

MadNeox


3 Answers

Implicit FTPS is not supported by the FtpWebRequest class (see here).

When EnableSsl is set to true, it actually triggers an AUTH TLS command to the server, asking to start an Explicit FTPS session.

In your case, you have to configure Filezilla Server to use Explicit FTPS. The procedure is documented on Filezilla Wiki

like image 175
Gabriel Boya Avatar answered Nov 15 '22 17:11

Gabriel Boya


I've encountered the same problem but for uploading a file, on ftpWriter.Close(). Also, I was unable to do a GetRequestStream after a successful PrinWorkingDirectory for example.

The problem seems to be a "Expect: 100-continue" in the post - while I didn't quite checked this, the problem is somewhere there.

I've tried every solution found on the internet : changing the KeepAlive to true, adding to the App.Config file

<system.net>
    <settings>
        <servicePointManager expect100Continue="false"/>
        <httpWebRequest useUnsafeHeaderParsing="true"/>
    </settings>
</system.net>

Nothing really worked.

I've spend a lot of time and trying different other third party libraries (idea I didn't like too much), until finally I came on a code that used the same classes and method but worked !! After analyzing the code, I've finally figured out : the code targeted .NET Framework 2.0 while my code was targeting .NET Framework 4.5. I seems that Microsoft did a little bug while passing from Framework 3.5 to Framework 4.

As it's not a solution to convert your new projects to target an old framework, you can create a dll for the FTP operations, pointing to the 3.5 .NET Framework, or, you can use third party libraries.

I'm maybe a little bit late, but it will probably help other frustrated developers on this matter, in the future.

like image 35
Marius-Lucian Dumitru Avatar answered Nov 15 '22 17:11

Marius-Lucian Dumitru


This is the time to migrate from ftp to sftp!
you can follow to, this code reference:

using Renci.SshNet;
using System.IO;
     private void UploadFileToSFTP()
            {
                try
                {
                    String sourcefile = @"C:\path\file.txt"; 
                    String host = @"000.000.000.0"; 
                    String username = @"usename"; 
                    String password = @"password"; 
                    int port = 22;
                    string destinationpath = "/var/www/html/path/public/destinationfolder";
                    using (SftpClient client = new SftpClient(host, port, username, password))
                    {
                        client.Connect();
                        client.ChangeDirectory(destinationpath);
                        using (FileStream fs = new FileStream(sourcefile, FileMode.Open))
                        {
                            client.BufferSize = 4 * 1024;
                            client.UploadFile(fs, Path.GetFileName(sourcefile));
                        }
                    }
        }
                catch (Exception)
                {
                    throw;
                }
            }

hope it would be helpful. Thank you!

like image 27
2 revs Avatar answered Nov 15 '22 18:11

2 revs