Sample code for 30+ languages & platforms
Android™

Connect to an FTP Server Without Logging In

See more FTP Examples

Demonstrates the Chilkat Ftp2.ConnectOnly method, which establishes the connection (including any proxy and TLS setup) and receives the greeting, but does not send USER or PASS. It takes no arguments; authentication is completed with LoginAfterConnectOnly.

Background: Splitting connect from login gives you a window between the two — to read the greeting, decide which account to use, or set up TLS options that depend on the server — that the all-in-one Connect does not. After ConnectOnly the control connection is open and CheckConnection returns true even though the session is not yet authenticated.

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.ConnectOnly method, which establishes the connection (including any proxy
    //  and TLS setup) and receives the greeting, but does NOT send USER or PASS.  It takes no
    //  arguments.  Authentication is completed separately with LoginAfterConnectOnly.

    CkFtp2 ftp = new CkFtp2();

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

    //  Connect without logging in.
    success = ftp.ConnectOnly();
    if (success == false) {
        Log.i(TAG, ftp.lastErrorText());
        return;
        }

    Log.i(TAG, "Connected (not yet logged in).");

    //  The greeting is now available; credentials can be set based on it, then authenticate.
    ftp.put_Username("myFtpLogin");
    ftp.put_Password("myPassword");

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

    Log.i(TAG, "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."
  }
}