Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Use an Existing Socket Connection for REST
See more REST Examples
Demonstrates Rest.UseConnection, which supplies an already-connected Socket as the transport for REST requests instead of calling Connect. This is useful when the connection must be established with specific socket options or shared across objects.
Background. Rest can operate over any connected socket. The second argument enables automatic reconnection so a dropped connection is transparently re-established between requests.
Chilkat Pascal (Lazarus/Delphi) Downloads
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.Rest,
Chilkat.Socket;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
socket: TSocket;
bSsl: Boolean;
rest: TRest;
bAutoReconnect: Boolean;
responseText: string;
begin
success := False;
// Demonstrates supplying an already-connected socket for the REST transport. This is useful when
// the connection must be established with specific socket options, or reused across objects.
socket := TSocket.Create;
bSsl := True;
success := socket.Connect('example.com',443,bSsl,5000);
if (success <> True) then
begin
WriteLn(socket.LastErrorText);
Exit;
end;
rest := TRest.Create;
// Use the connected socket as the transport. The 2nd argument enables automatic reconnection.
bAutoReconnect := True;
success := rest.UseConnection(socket,bAutoReconnect);
if (success = False) then
begin
WriteLn(rest.LastErrorText);
Exit;
end;
responseText := rest.FullRequestNoBody('GET','/api/status');
if (rest.LastMethodSuccess = False) then
begin
WriteLn(rest.LastErrorText);
Exit;
end;
WriteLn(responseText);
socket.Free;
rest.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.