Sample code for 30+ languages & platforms
Pascal (Lazarus/Delphi)

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 Pascal (Lazarus/Delphi) Downloads

Pascal (Lazarus/Delphi)
program ChilkatDemo;

// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.

{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}

uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  SysUtils,
  CkDllLoader,
  Chilkat.Ftp2;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  ftp: TFtp2;

begin
  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.

  ftp := TFtp2.Create;

  ftp.Hostname := 'ftp.example.com';
  ftp.Port := 21;

  //  First connect without logging in.
  success := ftp.ConnectOnly();
  if (success = False) then
    begin
      WriteLn(ftp.LastErrorText);
      Exit;
    end;

  //  Set credentials (for example, after inspecting the greeting), then log in.
  ftp.Username := 'myFtpLogin';
  ftp.Password := 'myPassword';

  success := ftp.LoginAfterConnectOnly();
  if (success = False) then
    begin
      WriteLn(ftp.LastErrorText);
      Exit;
    end;
  WriteLn('Logged in.');

  success := ftp.Disconnect();
  if (success = False) then
    begin
      WriteLn(ftp.LastErrorText);
      Exit;
    end;


  ftp.Free;

end;

// ---------------------------------------------------------------------------

begin

  try
    RunDemo;
  except
    on E: Exception do
      WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
  end;

  WriteLn;
  {$IFDEF MSWINDOWS}
  WriteLn('Press Enter to exit...');
  ReadLn;
  {$ENDIF}
end.