Soms hebben we een simpele manier nodig om informatie te downloaden vanuit het Internet, en tegenwoordig is daarbij het HTTPS protocol de standaard.
Helaas zijn de meest tools voor Lazarus afhankelijk van OpenSSL (Indy, Synapse, etc), wat dus wil zeggen dat je OpenSSL DLL's met jouw application moet leveren, iets wat ik zelf liever niet doe.
De fix hiervoor is gebruik maken van Windows Internet (WinINet) application programming interface (API), m.b.v. de volgende code die de wininet unit gebruikt (source):
Uses ... wininet ...
...
function GetWebPage(const Url: string): string;
var
NetHandle: HINTERNET;
UrlHandle: HINTERNET;
Buffer: array[0..1023] of Byte;
BytesRead: dWord;
StrBuffer: UTF8String;
begin
Result := '';
BytesRead := Default(dWord);
NetHandle := InternetOpen('Mozilla/5.0(compatible; WinInet)', INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
// NetHandle valid?
if Assigned(NetHandle) then
Try
UrlHandle := InternetOpenUrl(NetHandle, PChar(Url), nil, 0, INTERNET_FLAG_RELOAD, 0);
// UrlHandle valid?
if Assigned(UrlHandle) then
Try
repeat
InternetReadFile(UrlHandle, @Buffer, SizeOf(Buffer), BytesRead);
SetString(StrBuffer, PAnsiChar(@Buffer[0]), BytesRead);
Result := Result + StrBuffer;
until BytesRead = 0;
Finally
InternetCloseHandle(UrlHandle);
end
// o/w UrlHandle invalid
else
ShowMessage('Cannot open URL: ' + Url);
Finally
InternetCloseHandle(NetHandle);
end
// NetHandle invalid
else
raise Exception.Create('Unable to initialize WinInet');
end;
{$ENDIF}