Sample code for 30+ languages & platforms
Android™

Upgrade an FTP Control Channel to TLS (ConvertToTls)

See more FTP Examples

Demonstrates the Chilkat Ftp2.ConvertToTls method, which upgrades an already-connected clear FTP control channel to explicit TLS. It takes no arguments and is used with a manually staged connection created by ConnectOnly while Ssl is disabled.

Background: This exposes the explicit-FTPS AUTH TLS upgrade as a discrete step for the rare case where you need to do something on the clear connection first — inspect the greeting, apply a workaround — before securing it. In the normal case you simply set AuthTls and let Connect perform the upgrade automatically; this manual path exists for staged control.

Chilkat Android™ Downloads

Android™
// Important: Don't forget to include the call to System.loadLibrary
// as shown at the bottom of this code sample.
package com.test;

import android.app.Activity;
import com.chilkatsoft.*;

import android.widget.TextView;
import android.os.Bundle;

public class SimpleActivity extends Activity {

  private static final String TAG = "Chilkat";

  // Called when the activity is first created.
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    boolean success = false;

    //  Demonstrates the Ftp2.ConvertToTls method, which upgrades an already-connected clear FTP control
    //  channel to explicit TLS.  It takes no arguments.  It is used with a manually staged connection
    //  created by ConnectOnly while Ssl is disabled.

    CkFtp2 ftp = new CkFtp2();

    ftp.put_Hostname("ftp.example.com");
    ftp.put_Port(21);

    //  Connect in clear text first (Ssl is false).
    success = ftp.ConnectOnly();
    if (success == false) {
        Log.i(TAG, ftp.lastErrorText());
        return;
        }

    //  Upgrade the control channel to explicit TLS (AUTH TLS).
    success = ftp.ConvertToTls();
    if (success == false) {
        Log.i(TAG, ftp.lastErrorText());
        return;
        }

    //  Now authenticate over the encrypted control channel.
    ftp.put_Username("myFtpLogin");
    ftp.put_Password("myPassword");
    success = ftp.LoginAfterConnectOnly();
    if (success == false) {
        Log.i(TAG, ftp.lastErrorText());
        return;
        }

    Log.i(TAG, "Control channel upgraded to TLS and logged in.");

    success = ftp.Disconnect();
    if (success == false) {
        Log.i(TAG, ftp.lastErrorText());
        return;
        }


  }

  static {
      System.loadLibrary("chilkat");

      // Note: If the incorrect library name is passed to System.loadLibrary,
      // then you will see the following error message at application startup:
      //"The application <your-application-name> has stopped unexpectedly. Please try again."
  }
}