fpHttpClientAdvanced topic
Advanced TFPHTTPClient Features
Declaration
fpHttpClientAdvancedDescription
The TFPHTTPClient class supports advanced features like authentication, custom headers, cookies, proxy settings, and request/response event handling.
These features enable building robust HTTP client applications with full control over the HTTP communication.
program AdvancedHTTPExample;
{$mode objfpc}{$H+}
uses
SysUtils, Classes, fphttpclient, opensslsockets, base64;
procedure OnDataReceived(Sender: TObject; const ContentLength, CurrentPos: Int64);
begin
if ContentLength > 0 then
Write(Format('Progress: %d/%d bytes (%.1f%%)' + #13,
[CurrentPos, ContentLength, (CurrentPos/ContentLength)*100]));
end;
procedure OnPassword(Sender: TObject; var RepeatRequest: Boolean);
begin
Writeln('Authentication required - this is where you would prompt for credentials');
RepeatRequest := False; // Don't retry automatically
end;
var
Client: TFPHTTPClient;
Response: String;
AuthHeader: String;
begin
Client := TFPHTTPClient.Create(nil);
try
Writeln('=== Basic Authentication Example ===');
// HTTP Basic Authentication
AuthHeader := 'Basic ' + EncodeBase64('username:password');
Client.AddHeader('Authorization', AuthHeader);
// You can also use the Username/Password properties
// Client.UserName := 'testuser';
// Client.Password := 'testpass';
Writeln('=== Custom Headers Example ===');
Client.AddHeader('X-API-Key', 'your-api-key-here');
Client.AddHeader('X-Custom-Header', 'custom-value');
Client.AddHeader('Accept', 'application/json');
Writeln('=== Event Handlers Example ===');
Client.OnDataReceived := @OnDataReceived;
Client.OnPassword := @OnPassword;
Writeln('=== Cookie Management Example ===');
// Cookies are automatically managed between requests
Client.Cookies.Values['session_id'] := 'abc123';
Client.Cookies.Values['user_pref'] := 'dark_mode';
Writeln('=== Redirect Configuration ===');
Client.AllowRedirect := True;
Client.MaxRedirects := 10;
Writeln('=== Timeout Configuration ===');
Client.IOTimeout := 30000; // 30 seconds timeout
Client.ConnectTimeout := 10000; // 10 seconds connect timeout
Writeln('=== Proxy Configuration Example ===');
// Uncomment to use proxy
// Client.Proxy.Host := 'proxy.example.com';
// Client.Proxy.Port := 8080;
// Client.Proxy.UserName := 'proxy_user';
// Client.Proxy.Password := 'proxy_pass';
try
// Make request with all configured settings
Response := Client.Get('https://httpbin.org/headers');
Writeln('Request completed successfully!');
Writeln('Status Code: ', Client.ResponseStatusCode);
Writeln('Response Length: ', Length(Response));
// Access response headers
Writeln('Server: ', Client.GetHeader(Client.ResponseHeaders, 'Server'));
Writeln('Content-Type: ', Client.GetHeader(Client.ResponseHeaders, 'Content-Type'));
// Show cookies received from server
if Client.Cookies.Count > 0 then
begin
Writeln('Cookies received:');
Writeln(Client.Cookies.Text);
end;
except
on E: Exception do
begin
Writeln('Request failed: ', E.Message);
Writeln('Last status code: ', Client.ResponseStatusCode);
end;
end;
finally
Client.Free;
end;
end.An experiment, built from development snapshots. Not the official Free Pascal or Lazarus documentation.
