Delphi Internet Development with ICS

Introduction

Along with Indy and Synapse, ICS ('Internet Component Suite") is one of the most use open-source set of Internet components. It supports HTTP, SMTP, ICMP, etc. Support is available through the ICS mailing list.

Setup

Removing a previous version

  1. Component > Install Packages : Remove "Overbyte ICS for Delphi 7"
  2. From "C:\Program Files\Borland\Delphi7\Projects\Bpl\", delete OverbyteIcsDel70.bpl and OverbyteIcsDel70.dcp
  3. Tools > Environment Options > Library > Library Path: Remove path to previous version of ICS
  4. File > Close All

Note: "If you are reinstalling, or installing a new version, be sure to delete all dcu, and obj files from vc32 and internet directories. This applies to all Win32 versions. When installing a new version, always delete old dcu, obj, dcpil and always recompile everything ! Close everything before recompiling the library or packages.When installing a new version, be sure to unzip it in the same directory tree as the old one or you'll mess both versions."

Fresh install

  1. Unzip the ICS package
  2. Launch Delphi, File > Close all
  3. Open Delphi\Vc32\OverbyteIcsDel70.dpk (OverbyteIcsDel100Package.dpk is for D2006), Compile, Install, File > Close All
  4. Tools > Environment Options > Library > Library Path: Add path to Delphi\Vc32\ where DCU were just compiled

A new FPiette tab is displayed. Some samples are available in Delphi\Internet\ .

Connecting to a socket and TXing/RXing data

Uploading a file with FTP

FtpClient1.UserName:='mylogin';
FtpClient1.PassWord:='test';
FtpClient1.HostName:='ftp.acme.com';
FtpClient1.Passive := True;
FtpClient1.LocalFileName:='.\myfile.txt';
FtpClient1.HostDirName:='/';
FtpClient1.HostFileName := FtpClient1.LocalFileName;
try
FtpClient1.Connect;
FTPClient1.Binary := True;
FtpClient1.Put;
FtpClient1.Quit;
except
ShowMessage('Error uploading to FTP');
end;

Using FTP to upload all the files from a given directory

ICS comes with an FTP client component. There's also a big sample Internet\OverbyteIcsFtpTst1.pas.

Here's how to use the FTPClient component to list all the files in a given local directory, and upload them all to an FTP server (Note: doesn't recurse into sub-directories):

procedure TForm1.Button2Click(Sender: TObject);
var
MyFiles : TStringList;
Path : String;
SR: TSearchRec;
I : Integer;
begin
ListBox1.Clear;
If Length(LabeledEdit1.Text) = 0 then begin
ShowMessage('Choose source directory');
Exit;
end;
MyFiles := TStringList.Create;
try
Path := LabeledEdit1.Text;
if FindFirst(Path + '*.*', faAnyFile, SR) = 0 then begin
repeat
if (SR.Attr <> faDirectory) then begin
MyFiles.Add(SR.Name);
end;
until FindNext(SR) <> 0;
FindClose(SR);
end;
Button2.Enabled := False;
//Connect to FTP in BIN + PASSIVE
FtpClient1.UserName:='joe';
FtpClient1.PassWord:='mypass';
FtpClient1.HostName:='ftp.acme.com';
//Must exist!
FtpClient1.HostDirName:= LabeledEdit2.Text;
FtpClient1.Binary := True;
FtpClient1.Passive := True;
FtpClient1.Connect;
try
FtpClient1.Cwd;
try
//Upload each file
for I := 0 to MyFiles.Count-1 do begin
FtpClient1.LocalFileName:=LabeledEdit1.Text + MyFiles[i];
FtpClient1.HostFileName:=MyFiles[i];
ListBox1.Items.Add('Upload ' + MyFiles[i]);
PostMessage(Listbox1.Handle, wm_vscroll, SB_LINEDOWN, 0);
FtpClient1.Put;
end;
except
ShowMessage ('Error uploading files');
end;
FtpClient1.Quit;
except
ShowMessage ('Error connecting to FTP');
end;
Button2.Enabled := True;
finally
MyFiles.Free;
end;
end;

Downloading a web page and displaying its HTML source

procedure TForm1.Button1Click(Sender: TObject);
begin
With HttpCli1 do begin
URL := 'www.google.com';
RequestVer := '1.1';
RcvdStream := TMemoryStream.Create;
try
Get;
except
ShowMessage('GET Failed !');
RcvdStream.Free;
Exit;
end;
RcvdStream.Seek(0,0);
Memo1.Lines.LoadFromStream(RcvdStream);
RcvdStream.Free;
end;
end;

Authenticating with POST + cookie

Downloading a web page through a local proxy

First, create a new application, and add an HttpCli component from the FPiette tab. Next, add a label and a push button, and paste the following code:

implementation
const
MyURL = 'http://www.acme.com';
MyPROXY = 'localhost';
MyPORT = '50000';
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
I : Integer;
Response : String;
begin
With HttpCli1 do begin
URL := MyURL;
Proxy := MyPROXY;
ProxyPort := MyPORT;
RequestVer := '1.1';
RcvdStream := TMemoryStream.Create;
try
Get;
except
Response := 'GET Failed !' + #13#10;
Response := Response + 'StatusCode = ' + IntToStr(HttpCli1.StatusCode) + #13#10;
Response := Response + 'ReasonPhrase = ' + HttpCli1.ReasonPhrase;
ShowMessage(Response);
RcvdStream.Free;
Exit;
end;
Response := 'StatusCode = ' + IntToStr(StatusCode);
for I := 0 to RcvdHeader.Count - 1 do begin
Response := Response + RcvdHeader.Strings[I] + #13#10;
end;
Label1.Caption := Response;
RcvdStream.Seek(0,0);
Memo1.Lines.LoadFromStream(RcvdStream);
RcvdStream.Free;
end;
end;

Resources

AltStyle によって変換されたページ (->オリジナル) /