{$IFDEF UNIX}
function GetMicrochipPage(const URL: string): string;
var
Client: TFPHttpClient;
{$IFDEF DARWIN}
MsgStr: String;
{$ENDIF}
begin
Client := TFPHttpClient.Create(nil);
Try
Client.AllowRedirect := true;
Client.AddHeader('User-Agent', 'Mozilla/5.0(compatible; fpweb)');
Result := Client.Get(URL);
except
on E: Exception do
{$IFDEF DARWIN}
begin
MsgStr := 'Retrieval of: ' + URL + LineEnding
+ 'Failed with error: ' + E.Message + LineEnding
+ 'HTTP code: ' + IntToSTr(Client.ResponseStatusCode);
ShowAlertSheet(Form1_Main.Handle, 'Alert', MsgStr);
end;
{$ENDIF}
{$IFNDEF DARWIN}
ShowMessage('Retrieval of: ' + URL + LineEnding
+ 'Failed with error: ' + E.Message + LineEnding
+ 'HTTP code: ' + IntToSTr(Client.ResponseStatusCode));
{$ENDIF}
end;
end;
{$ENDIF}
{$IFDEF WINDOWS}
// Need to use Windows WinInet to avoid issue with HTTPS
// needing two OpenSSL DLLs to be provided with application
// if using TFPHttpClient.
// The WinINet API also gets any connection and proxy settings
// set by Internet Explorer. Blessing or curse?
function GetMicrochipPage(const Url: string): string;
var
NetHandle: HINTERNET;
UrlHandle: HINTERNET;
Buffer: array[0..1023] of Byte;
BytesRead: dWord;
StrBuffer: UTF8String;
begin
Result := '';
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}