简体   繁体   中英

How to configure TidFTP with SSL?

I have a little FTP client I wrote to manage my files on my bluehost.com account. Now that I have migrated to a new hosting company, my program will connect, but that is all it will do.

After connecting, I tried to get an inventory of the root files; the program hangs for a bit and finally produces a "read time out".

After researching, I came to the conclusion (possibly incorrectly) that this had something to do with SSL (which I have available to me through my new host). I added a TIdSSLIOHandlerSocketOpenSSL component to my form, assigned it to myFTP and set UseTLS = utUseExplicitTLS .

Now, when I try to connect, I get an 10054 error. I have not been able to find any examples as to how to configure this connection.

Here is the code:

unit ct_FTP_Test;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes,
  Vcl.Graphics, Vcl.Forms, Vcl.StdCtrls, Vcl.Controls, IdTCPConnection, IdTCPClient,
  IdExplicitTLSClientServerBase, IdFTP, IdBaseComponent, IdComponent, IdIOHandler,
  IdIOHandlerSocket, IdIOHandlerStack, IdSSL, IdSSLOpenSSL, IdAntiFreezeBase,
  IdFTPCommon, IdAntiFreeze;

type
  TForm1 = class(TForm)
    btnConnectFTP: TButton;
    Label2: TLabel;
    Label4: TLabel;
    mem: TMemo;
    btnShowFolder: TButton;
    idSSLIOHandlerSocketOpenSSL: TIdSSLIOHandlerSocketOpenSSL;
    myFTP: TIdFTP;
    edHost: TEdit;
    edUser: TEdit;
    edPassword: TEdit;
    Label1: TLabel;
    Label3: TLabel;
    Label5: TLabel;
    idAntiFreeze: TIdAntiFreeze;
    edPort: TEdit;
    Label6: TLabel;
    ckbUseSSL: TCheckBox;
    procedure btnConnectFTPClick(Sender: TObject);
    procedure btnShowFolderClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    procedure GetFolderInventory;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.GetFolderInventory;
var DirList : TStringList;
begin
  if (not myFTP.Connected) then
    begin
      Label2.Caption := 'You are not connected...';
      Exit;
    end;

  try
    try
      Label4.Caption := 'Going to root...';
      myFTP.ChangeDir('/');
      Label4.Caption := 'At root, getting DirList...';

      DirList := TStringList.Create;
      myFTP.List(DirList, '', True);

      Label2.Caption := '';
      Label4.Caption := IntToStr(DirList.Count) + ' files and folders found';

      mem.Lines.Assign(DirList);
    except on e:Exception do
      begin
        Label4.Caption := 'Unable to show contents';
        Label2.Caption := e.Message;
      end;
    end;
  finally
    FreeAndNil(DirList);
  end;
end;

procedure TForm1.btnShowFolderClick(Sender: TObject);
begin
  GetFolderInventory;
end;

procedure TForm1.btnConnectFTPClick(Sender: TObject);
begin
  try
    Label2.Caption := '';
    Label4.Caption := '';

    if (myFTP.Connected) then
      myFTP.Disconnect;

    if (edHost.Text = '') then
      begin
        edHost.SetFocus;
        Label4.Caption := 'Host required.';
        Exit;
      end;

    if (edUser.Text = '') then
      begin
        edUser.SetFocus;
        Label4.Caption := 'User required.';
        Exit;
      end;

    if (edPassword.Text = '') then
      begin
        edPassword.SetFocus;
        Label4.Caption := 'Password required.';
        Exit;
      end;

    if (edPort.Text = '') then
      begin
        edPort.SetFocus;
        Label4.Caption := 'Port required.';
        Exit;
      end;

    myFTP.Host := edHost.Text;
    myFTP.Username := edUser.Text;
    myFTP.Password := edPassword.Text;
    myFTP.Port := StrToInt(edPort.Text);

    if (ckbUseSSL.Checked) then
      begin
        myFTP.UseTLS := utUseExplicitTLS;
        myFTP.DataPortProtection := ftpdpsPrivate;
        myFTP.Passive := True;
      end
    else
      begin
        myFTP.DataPortProtection := ftpdpsClear;
        myFTP.UseTLS := utNoTLSSupport;
        myFTP.Passive := False;
      end;


    myFTP.Connect;
    Label4.Caption := 'Connected';
    Label2.Caption := '';
  except on e:Exception do
    begin
      Label4.Caption := 'Failed to connect';
      Label2.Caption := e.Message;
    end;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Label2.Caption := '';
  Label4.Caption := '';
end;

end.

Here's the dfm:

object Form1: TForm1
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 568
  ClientWidth = 704
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 13
  object Label2: TLabel
    Left = 120
    Top = 101
    Width = 496
    Height = 44
    AutoSize = False
    Caption = '[...]'
    WordWrap = True
  end
  object Label4: TLabel
    Left = 120
    Top = 72
    Width = 20
    Height = 13
    Caption = '[...]'
  end
  object Label1: TLabel
    Left = 120
    Top = 37
    Width = 22
    Height = 13
    Caption = 'Host'
  end
  object Label3: TLabel
    Left = 272
    Top = 37
    Width = 22
    Height = 13
    Caption = 'User'
  end
  object Label5: TLabel
    Left = 456
    Top = 37
    Width = 32
    Height = 13
    Caption = 'PWord'
  end
  object Label6: TLabel
    Left = 605
    Top = 37
    Width = 20
    Height = 13
    Caption = 'Port'
  end
  object btnConnectFTP: TButton
    Left = 32
    Top = 32
    Width = 75
    Height = 25
    Caption = 'Connect'
    TabOrder = 0
    OnClick = btnConnectFTPClick
  end
  object mem: TMemo
    Left = 113
    Top = 160
    Width = 503
    Height = 377
    ScrollBars = ssVertical
    TabOrder = 1
    WordWrap = False
  end
  object btnShowFolder: TButton
    Left = 32
    Top = 160
    Width = 75
    Height = 25
    Caption = 'Show Folder'
    TabOrder = 2
    OnClick = btnShowFolderClick
  end
  object edHost: TEdit
    Left = 148
    Top = 32
    Width = 105
    Height = 21
    TabOrder = 3
  end
  object edUser: TEdit
    Left = 300
    Top = 32
    Width = 137
    Height = 21
    TabOrder = 4
  end
  object edPassword: TEdit
    Left = 495
    Top = 32
    Width = 90
    Height = 21
    TabOrder = 5
  end
  object edPort: TEdit
    Left = 633
    Top = 32
    Width = 40
    Height = 21
    NumbersOnly = True
    TabOrder = 6
    Text = '21'
  end
  object ckbUseSSL: TCheckBox
    Left = 40
    Top = 72
    Width = 57
    Height = 17
    Caption = 'Use SSL'
    TabOrder = 7
  end
  object idSSLIOHandlerSocketOpenSSL: TIdSSLIOHandlerSocketOpenSSL
    MaxLineAction = maException
    DefaultPort = 0
    ReadTimeout = 60000
    SSLOptions.Mode = sslmClient
    SSLOptions.VerifyMode = []
    SSLOptions.VerifyDepth = 0
    Left = 69
    Top = 318
  end
  object myFTP: TIdFTP
    IOHandler = idSSLIOHandlerSocketOpenSSL
    IPVersion = Id_IPv4
    ConnectTimeout = 0
    NATKeepAlive.UseKeepAlive = False
    NATKeepAlive.IdleTimeMS = 0
    NATKeepAlive.IntervalMS = 0
    ProxySettings.ProxyType = fpcmNone
    ProxySettings.Port = 0
    Left = 22
    Top = 278
  end
  object idAntiFreeze: TIdAntiFreeze
    Left = 24
    Top = 216
  end
end

Any help will be greatly appreciated.

尝试通过设置正确的属性让所有SLL methodes /类型(不记得实际的属性名称)的TIdSSLIOHandlerSocketOpenSSL

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM