I am trying to make an FTPS connection to a FileZilla server with C#, but the C# app never logs in to the server, the server closes the connection, and I receive the following error in C#:
The underlying connection was closed: The server committed a protocol violation.
My C# code is like so:
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
var req = (FtpWebRequest)WebRequest.Create("ftp://<my IP>:<my port>/");
req.UsePassive = true;
req.UseBinary = true;
req.EnableSsl = true;
req.KeepAlive = true;
req.Credentials = new NetworkCredential("Puff", "Daddy");
req.Method = WebRequestMethods.Ftp.ListDirectory;
using (var response = (FtpWebResponse)req.GetResponse())
{
//Do Something
}
Upon a connection attempt the FileZilla Server Interface prints the following:
The C# network log reads like so:
System.Net.Sockets Verbose: 0 : [6424] Socket#54135081::Socket(AddressFamily#23)
System.Net.Sockets Verbose: 0 : [6424] Exiting Socket#54135081::Socket()
System.Net.Sockets Verbose: 0 : [6424] DNS::TryInternalResolve(<FTP Server IP>)
System.Net.Sockets Verbose: 0 : [6424] Socket#5773521::Connect(<FTP Server IP>:<FTP Port>#-1925092664)
System.Net.Sockets Information: 0 : [6424] Socket#5773521 - Created connection from <local IP>:61169 to <Server IP>:<Server Port>.
System.Net.Sockets Verbose: 0 : [6424] Exiting Socket#5773521::Connect()
System.Net.Sockets Verbose: 0 : [6424] Socket#54135081::Close()
System.Net.Sockets Verbose: 0 : [6424] Socket#54135081::Dispose()
System.Net.Sockets Verbose: 0 : [6424] Exiting Socket#54135081::Close()
System.Net Information: 0 : [6424] FtpControlStream#63094882 - Created connection from <Local IP>:61169 to <Server IP>:<Server Port>.
System.Net Information: 0 : [6424] Associating FtpWebRequest#59817589 with FtpControlStream#63094882
System.Net.Sockets Verbose: 0 : [6424] Socket#5773521::Receive()
System.Net.Sockets Verbose: 0 : [6424] Data from Socket#5773521::Receive
System.Net.Sockets Verbose: 0 : [6424] 00000000 : :
System.Net.Sockets Verbose: 0 : [6424] Exiting Socket#5773521::Receive() -> Int32#0
System.Net.Sockets Verbose: 0 : [6424] Socket#5773521::Dispose()
System.Net Information: 0 : [6424] FtpWebRequest#59817589::(Releasing FTP connection#63094882.)
System.Net Error: 0 : [6424] Exception in FtpWebRequest#59817589::GetResponse - The underlying connection was closed: The server committed a protocol violation.
I have successfully made a connection with the same parameters from the same PC with FileZilla client, so I know it should work.
I have added these lines to the App.Config.
<system.net>
<settings>
<servicePointManager expect100Continue="false"/>
<httpWebRequest useUnsafeHeaderParsing="true"/>
</settings>
</system.net>
I have also tried targeting .NET Framework 3.5 and 2.0, but both had the same results. Currently I am running .NET 4.6. Do I have to install the servers certificate or something? Or is the code wrong?
The answer came to me about 2 minutes after I posted this question. I'll leave it around in case anyone else has these struggles (I sometimes like to entertain the idea that someone shares my problems, however unlikely that may be).
In the "FTP over TLS settings" dialog on the FileZilla server I had configured a port to listen for implicit FTP or TLS (default is 990). This was the port I used in the FZ Client FTPS connection. I was also trying to use this port from C#, which doesn't work, because the connection is explicit FTPS. I had to use the port specified in the General Settings as the listen port (default 21).
I had actually tried this before posting the question, but forgot to apply the router's configuration so it didn't work, "Der".