Home > Mobile >  FTPSClient fails to connect with MalformedServerReplyException
FTPSClient fails to connect with MalformedServerReplyException

Time:04-23

I'm using the following code:

val ftpClient = FTPSClient()
ftpClient.connect(host, port)

And getting the following error:

org.apache.commons.net.MalformedServerReplyException: Could not parse response code.
Server Reply: SSH-2.0-BIS FileExchange Gateway 2.70.1

It's as if the client is not even speaking SSL when it connects but I'm stumped.

Attempt to "coerce" SSL sockets produce no difference:

val sslContext = SSLContext.getInstance("SSL").apply {
    init(null, arrayOf<TrustManager>(TrustManagerUtils.getAcceptAllTrustManager()), null)
}
val ftpClient = FTPSClient(sslContext)
ftpClient.connect(host, port)

Setting the socket factory produced a socket closed exception:

ftpClient.setSocketFactory(sslContext.socketFactory)

I should note that I can operate the endpoint using sftp so I can infer that the endpoint works correctly.

CodePudding user response:

FTPSClient implements FTPS, i.e. FTP over SSL/TLS. It is expected that you connect to an FTP server with TLS support. The error message indicates though that you connect to a SSH server, not a FTP server. This means that you are probably trying to use SFTP (file transfer over SSH) not FTPS. Thus, there is a mismatch in the protocol spoken between client and server.

For how to use SFTP instead of FTPS see for example How to retrieve a file from a server via SFTP?.

CodePudding user response:

This finally worked for me and some of it may have to do with the way the endpoint is configured but I'm not sure.

ftpClient.connect(host)
// This is very important!
ftpClient.execPROT("P")
ftpClient.login(username, password)
// This is very important!
ftpClient.enterLocalPassiveMode()
  • Related