Sample code for 30+ languages & platforms
Android™

Log In After ConnectOnly on an FTP Server

See more FTP Examples

Demonstrates the Chilkat Ftp2.LoginAfterConnectOnly method, which authenticates a control connection previously established by ConnectOnly. It takes no arguments, sends USER, PASS, and ACCT when required, then performs normal post-login initialization.

Background: This is the second half of the two-step connect. Beyond sending the credentials, it runs the same post-login setup Connect would — for example issuing TYPE I and any auto-negotiated features — so the session ends up in the same ready state either way. The value of the split is entirely in what you can do in the gap between the two calls.

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.LoginAfterConnectOnly method, which authenticates a control connection
    //  previously established by ConnectOnly.  It takes no arguments and sends USER, PASS, and (when
    //  required) ACCT, then performs normal post-login initialization.

    CkFtp2 ftp = new CkFtp2();

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

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

    //  Set credentials (for example, after inspecting the greeting), then log in.
    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."
  }
}