Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Disconnect from a REST Server
See more REST Examples
Demonstrates Rest.Disconnect, which closes the current HTTP connection. The argument is the maximum number of milliseconds to wait for the close to complete.
Background. Connections are normally kept alive for reuse. Disconnect is used when an application wants to explicitly release the connection rather than relying on keep-alive or object destruction.
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;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
rest: TRest;
bTls: Boolean;
bAutoReconnect: Boolean;
responseText: string;
bDisconnected: Boolean;
begin
success := False;
rest := TRest.Create;
// Connect to the REST server. The 3rd argument enables TLS (use True for HTTPS on port 443),
// and the 4th enables automatic reconnection if the connection is dropped between requests.
bTls := True;
bAutoReconnect := True;
success := rest.Connect('example.com',443,bTls,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);
// Close the connection. The argument is the maximum number of milliseconds to wait for the
// close to complete.
bDisconnected := rest.Disconnect(200);
if (bDisconnected <> True) then
begin
WriteLn('The disconnect did not complete within the wait time.');
end;
WriteLn('Done.');
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.