Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
HTTP HEAD Request
See more HTTP Examples
Sends an HTTP HEAD request and gets the response.Note: The response to an HTTP HEAD request is always just the response header. The reponse body is always 0 length (thus the reason it's called a "HEAD" request..)
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.HttpResponse,
Chilkat.Http;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
http: THttp;
resp: THttpResponse;
numHeaderFields: Integer;
i: Integer;
begin
success := False;
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
http := THttp.Create;
// If the URL uses "https://", then the connection will be TLS.
// Otherwise it will be TCP.
// A failure is when we don't get any response. It could be a timeout, an inability to connect, etc.
// For example, a "404 Not Found" response is still a response, and thus deemed success in terms of the API..
resp := THttpResponse.Create;
success := http.HttpNoBody('HEAD','https://example-code.com/',resp);
if (success = False) then
begin
WriteLn(http.LastErrorText);
Exit;
end;
// Examine the response.
WriteLn('Status Code = ' + resp.StatusCode);
WriteLn('Status Line = ' + resp.StatusLine);
WriteLn('Status Text = ' + resp.StatusText);
WriteLn('Full Response Header:');
WriteLn(resp.Header);
WriteLn('----');
numHeaderFields := resp.NumHeaderFields;
WriteLn('Num Header Fields: ' + numHeaderFields);
for i := 0 to numHeaderFields - 1 do
begin
WriteLn(resp.GetHeaderName(i) + ': ' + resp.GetHeaderValue(i));
end;
http.Free;
resp.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.