How to use WinInet from Lazarus? Download a file and POST lazarus / 38-Networking and Web Programming ---------------------------------------------------------------------- Igor · 2018-10-29 12:44 Hi, Is it possible to use WinInet API from Lazarus to download a file from HTTPS server, or perform POST query (to implement online activation for my product). I tried fphttpclient, but it requires OpenSSL dlls. ---------------------------------------------------------------------- sstvmaster · 2018-10-29 13:01 You can download OpenSSL file here: https://indy.fulgan.com/SSL/ (openssl-1.0.2p-...) Download version win32 or win64, unzip and copy libeayXX.dll and ssleayXX.dll in your program folder. ---------------------------------------------------------------------- Igor · 2018-10-29 13:31 Thanks, Yes, it worked. But is it possible to use WinInet in Lazarus? ---------------------------------------------------------------------- Thaddy · 2018-10-29 13:44 Yes it is!... but only on Windows. (I believe it is also deprecated by microsoft, at least for serverside applications. See: https://msdn.microsoft.com/library/windows/desktop/aa384273  and https://docs.microsoft.com/en-us/windows/desktop/WinInet/enabling- internet-functionality ) Unless you have a reason, don't use it since it is not cross-platform. fcl-net / fcl-web and synapse and indy are all cross-platform. ---------------------------------------------------------------------- Igor · 2018-10-29 13:55 Thanks for you reply! What is best and simpliest cross-platform API (Win + macOS) just to download a file, or perform POST request to a server? ---------------------------------------------------------------------- fred · 2018-10-29 14:09 Simpliest would be SimpleGet: http://wiki.lazarus.freepascal.org/fphttpclient ---------------------------------------------------------------------- Thaddy · 2018-10-29 15:11 Quote from: fred on October 29, 2018, 03:09:36 pmSimpliest would be SimpleGet: http://wiki.lazarus.freepascal.org/fphttpclient Yes, maybe, but atm. the synapse solution is still simpler since simpleget does not allow for redirects (yet). So you can get the dreaded 403... As I wrote before, this one allows https, redirects and works almost always;Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$ifdef fpc}{$mode delphi}{$H+}{$endif}uses classes, httpsend, ssl_openssl;var  l:Tstrings;begin  try    l:=Tstringlist.create;    if HttpGetText('https://www.google.com',L) then  // this has a redirection as well as ssl enabled.      writeln(l.text);  finally    l.free;  end;end. That said, the same can be done will a slight modification of the SimpleGet code. I'll see if I can submit a patch. In the above code you may want to use httpgetbinary to download a file, btw. And use a stream. This was just for illustration. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$ifdef fpc}{$mode delphi}{$H+}{$endif}uses classes, httpsend, ssl_openssl;var  S:TStream;  l:Tstrings;begin    S:=TFilestream.Create('testme.bin', fmCreate or fmOpenWrite);     L:=TStringlist.create;    try    if HttpGetBinary('https://www.google.com',S) then    begin      S.Free;    L.LoadFromFile('testme.bin');      writeln(l.text);    end;  finally    l.free;  end;end. Note that I mixed the two but the essential part is how to download in binary format. (Q & D) But cross-platform. ---------------------------------------------------------------------- Igor · 2018-10-30 11:16 Thanks for your recommendations! I will check it. ---------------------------------------------------------------------- Hansaplast · 2019-11-27 12:51 A little late, but I was looking for an "easy" way to download a file from a webserver using HTTPS and ran into this topic. Under more recent macOS versions (at least as of Mojave), fphttpclient will not work. Apple does not seem to like the OpenSSL library (default macOS setup!): Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Invalid dylib load. Clients should not load the unversioned libcrypto dylib as it does not have a stable ABI. As an alternative for macOS users, use the unit "ns_url_request" by Phil Hess (can be found here - Phil's Mac related page). This does not require any extra libraries (beyond what comes with macOS). A quick (and sloppy) function pulling in HTTPS content as a string: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uses ... ns_url_request ... // Note: "ns_url_request" uses Phil's "NSHelpers" unit.  ...  function TForm1.GetURLContent(aURL:string):string;var  HTTP: TNSHTTPSendAndReceive;begin  HTTP := TNSHTTPSendAndReceive.Create;  HTTP.Method   := 'GET';  HTTP.Address  := aURL;  HTTP.SendAndReceive(Result);  HTTP.Free;end; ---------------------------------------------------------------------- trev · 2019-12-04 12:31 Quote from: Hansaplast on November 27, 2019, 01:51:25 pmUnder more recent macOS versions (at least as of Mojave), fphttpclient will not work. Works for me in macOS (Mojave, Catalina), FreeBSD and Linux. Note: I'm using trunk (see below). ---------------------------------------------------------------------- trev · 2019-12-04 12:33 Quote from: Igor Kokarev on October 29, 2018, 01:44:12 pmIs it possible to use WinInet API from Lazarus to download a file from HTTPS server, or perform POST query (to implement online activation for my product). Yes! See below: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$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 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} ---------------------------------------------------------------------- Hansaplast · 2019-12-04 12:45 Hmm, interesting. My Mac's (Catalina) complain with the above error  ... Phil wrote a small document on sandboxing and macOS (link) where he states as well; QuoteOpenSSL on Mac has been deprecated for a long time, so you should be using one of Apple's APIs instead And he refers to an Apple document from June 2018, indicating OpenSSL is deprecated and its use is "strongly" discouraged: Quote ... OpenSSL is deprecated in macOS and is not provided in iOS. Use of the Apple-provided OpenSSL libraries by apps is strongly discouraged. Granted; Sandbox is not the same as non-Sandbox apps, but I did run into issues with my regular apps as well (not sandboxed, not in the Apple Store) and Phils code resolved it. p.s. Thanks Trev for the WinInet code example - I'll give that a try as well (under Windows of course) ---------------------------------------------------------------------- trev · 2019-12-04 12:56 Quote from: Hansaplast on December 04, 2019, 01:45:48 pmHmm, interesting. My Mac's (Catalina) complain with the above error  Here's the code I use without any issues on macOS, FreeBSD and Linux: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$IFDEF UNIX}function GetWebPage(const aURL: string): string;var  Client: TFPHttpClient;begin  Client := TFPHttpClient.Create(nil);   Try    Client.AllowRedirect := true;    Client.AddHeader('User-Agent', 'Mozilla/5.0(compatible; fpweb)');    Result := Client.Get(aURL);  except      on E: Exception do           ShowMessage('Retrieval of: ' + aURL + LineEnding                       + 'Failed with error: ' + E.Message + LineEnding                       + 'HTTP code: ' + IntToSTr(Client.ResponseStatusCode));  end;end;{$ENDIF} Quotep.s. Thanks Trev for the WinInet code example - I'll give that a try as well (under Windows of course)  Glad to help ---------------------------------------------------------------------- MISV · 2019-12-05 20:58 Have you checked this thread. https://community.idera.com/developer-tools/platforms/f/macos- platform/70310/issue-with-using-indy-on-catalina It is two months old where Remy mentions having introduced a fix / new function call in Indy ---------------------------------------------------------------------- BeniBela · 2019-12-10 11:49 My Internet Tools use wininet for downloading