Skip to content

Instantly share code, notes, and snippets.

@adnan360
Created March 12, 2019 12:35
Show Gist options
  • Save adnan360/3f599395c3e5fbe7479e6be4b84b2a7a to your computer and use it in GitHub Desktop.
Save adnan360/3f599395c3e5fbe7479e6be4b84b2a7a to your computer and use it in GitHub Desktop.
Get URL Content in Lazarus - using Synapse
function DownloadHTTP(URL: string; SaveToFile: boolean=False; TargetFile: string=''): string;
var
HTTPGetResult: Boolean;
HTTPSender: THTTPSend;
S: string;
begin
// Result will be:
// - empty ('') when it has failed
// - filename when the file has been downloaded successfully
// - content when the content SaveToFile is set to False
Result := '';
HTTPSender := THTTPSend.Create;
try
HTTPGetResult := HTTPSender.HTTPMethod('GET', URL);
if (HTTPSender.ResultCode >= 100) and (HTTPSender.ResultCode<=299) then begin
if SaveToFile = true then begin
HTTPSender.Document.SaveToFile(TargetFile);
Result := TargetFile;
end else begin
SetLength(S, HTTPSender.Document.Size);
HTTPSender.Document.Read(S[1], Length(S));
Result:=S;
end;
end;
finally
HTTPSender.Free;
end;
end;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment