Skip to content

Instantly share code, notes, and snippets.

@synopse
Created August 8, 2024 14:47
Show Gist options
  • Save synopse/40f0440ba253e6345141ddf9b8ee6e60 to your computer and use it in GitHub Desktop.
Save synopse/40f0440ba253e6345141ddf9b8ee6e60 to your computer and use it in GitHub Desktop.
THttpClientSocket sample from mORMot end user
function InternalGet(AURL: String; var AOutStatus: Integer; var AErrorType: TURLErrorType; AHeadOnly: boolean): String;
var
URI: TURI;
params: THttpClientSocketWGet;
procedure InitParams;
begin
with params do
begin
Clear;
OnProgress := @InternalHandleProgress;
KeepAlive := KeepAliveTime;
TimeOutSec := KeepAliveTime div 1000;
case OAuthType of
duaNone: ;
duaBasic: Client.BasicAuthUserPassword := OUserName + ':' + OPassword;
duaBearer: Client.AuthBearer := OBearer;
end;
Header := 'Accept-Encoding: gzip';
end;
end;
begin
try
if not ((URI.From(AURL)) and ((URI.Scheme = 'http') or (URI.Scheme = 'https'))) then
Exit;
Client := THttpClientSocket.Create(KeepAliveTime);
with Client do
begin
OnAfterRequest := @HandleAfterRequest;
RedirectMax := 10;
TLS.IgnoreCertificateErrors := True;
OpenBind(URI.Server, URI.Port, false, URI.Https);
KeepAlive := True;
end;
InitParams;
StatusCode := Client.Head(URI.Address);
if ((StatusCode >= 200) and (StatusCode < 300)) then
begin
if (not AHeadOnly) then
begin
Client.Close;
Client.OpenBind(URI.Server, URI.Port, false, URI.Https);
ODownloadFileName := Client.WGet(URI.Address, ODownloadFileName, params);
end;
end;
finally
if Assigned(Client) then
FreeAndNil(Client);
end;
end;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment