Created
August 8, 2024 14:47
-
-
Save synopse/40f0440ba253e6345141ddf9b8ee6e60 to your computer and use it in GitHub Desktop.
THttpClientSocket sample from mORMot end user
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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