Created
March 12, 2019 12:35
-
-
Save adnan360/3f599395c3e5fbe7479e6be4b84b2a7a to your computer and use it in GitHub Desktop.
Get URL Content in Lazarus - using Synapse
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 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