The underlying connection was closed. The server committed a protocol violation
Asked Answered
B

3

8

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

Breuer answered 9/10, 2014 at 12:45 Comment(2)
Do you have access to the Filezilla Server ? Are you sure the certificate is valid ? Did you configure it properly to use explicit FTPSStatuary
Yes I have access to the server. The certificate is also valid and the explicit FTPS is checked. I am using port 990 that refers to implicit SSL/TLS connection. Is it supported in FtpWebRequest ?Breuer
S
6

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

Statuary answered 9/10, 2014 at 13:14 Comment(2)
that's true. I will consider try 3rd party libraries to use both explicit / implicit FTPS session. Thanks for help.Breuer
I will... Thanks again for support and help. Have a nice day !Breuer
T
4

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.

Term answered 9/12, 2014 at 11:0 Comment(1)
If this is a bug in the client, I wonder why it only seems to happen when connecting to filezilla server? My code works fine with other serversSible
L
1

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!

Lach answered 9/10, 2014 at 12:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.