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?