Recent

Author Topic: GetURLText  (Read 2630 times)

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2079
  • Fifty shades of code.
    • Delphi & FreePascal
Re: GetURLText
« Reply #15 on: March 06, 2023, 07:57:07 pm »
I also tried KodeZwerg's solution using Synapse. No luck yet.
Just because you edited your post, i tested my source on http without having any problems.
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2079
  • Fifty shades of code.
    • Delphi & FreePascal
Re: GetURLText
« Reply #16 on: March 06, 2023, 08:04:39 pm »
@GetMem, on Windows 64bit you need to have different filenames!
Code: Pascal  [Select][+][-]
  1.   {$IFDEF WIN64}
  2.   LibCrypto := ParamPath + 'libcrypto-1_1-x64.dll';
  3.   LibSSL := ParamPath + 'libssl-1_1-x64.dll';
  4.   {$ENDIF}
  5.   {$IFDEF WIN32}
  6.   LibCrypto := ParamPath + 'libcrypto-1_1.dll';
  7.   LibSSL := ParamPath + 'libssl-1_1.dll';
  8.   {$ENDIF}
That works for Windows 64bit and 32bit.
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

balazsszekely

  • Guest
Re: GetURLText
« Reply #17 on: March 06, 2023, 08:06:21 pm »
Quote
I tried your example on Windows, it tells me that OpenSSL is not available.
Then copy libcrypto-1_1.dll and libssl-1_1.dll in the same folder as your executable(only needed on windows). Download the libraries from here:
32 bit: http://packages.lazarus-ide.org/openssl-1.1.1o-i386-win32.zip
64 bit: 'http://packages.lazarus-ide.org/openssl-1.1.1o-x64_86-win64.zip

Then the following code should cross platform:
Code: Pascal  [Select][+][-]
  1. uses fphttpclient, opensslsockets;
  2.  
  3. function GetURL(URL: String; var URLRes: String): Boolean;
  4. var
  5.   SS: TStringStream;
  6.   FPHTTPClient: TFPHTTPClient;
  7. begin
  8.   Result := False;
  9.   FPHTTPClient := TFPHTTPClient.Create(nil);
  10.   try
  11.     FPHTTPClient.AllowRedirect := True;
  12.     SS := TStringStream.Create('');
  13.     try
  14.       Screen.Cursor := crHourglass;
  15.       try
  16.         FPHTTPClient.Get(URL, SS);
  17.       finally
  18.         Screen.Cursor := crDefault;
  19.       end;
  20.       if FPHTTPClient.ResponseStatusCode = 200 then
  21.       begin
  22.         URLRes := SS.DataString;
  23.         Result := True;
  24.       end;
  25.     finally
  26.       SS.Free;
  27.     end;
  28.   finally
  29.     FPHTTPClient.Free;
  30.   end;
  31. end;

Quote
Maybe I should just give up.
No! Don't give up.  :)

balazsszekely

  • Guest
Re: GetURLText
« Reply #18 on: March 06, 2023, 08:17:27 pm »
@KodeZwerg
Quote
@GetMem, on Windows 64bit you need to have different filenames!
Yes, you're right, the name of the 64 bit version is wrong. Thanks for catching that! I will modify my previous post.

VTwin

  • Hero Member
  • *****
  • Posts: 1215
  • Former Turbo Pascal 3 user
Re: GetURLText
« Reply #19 on: March 06, 2023, 08:39:49 pm »
No! Don't give up.  :)

 :D Thanks! I'll keep at it, but already spent half a day. Still no luck.

Downloading a DLL with my software is probably a non-starter. More and more end users are on locked down computers, with IT freaking out every time someone sneezes on the keyboard. I could probably include it in the installation zip file, but that adds an additional complexity.
“Talk is cheap. Show me the code.” -Linus Torvalds

Free Pascal Compiler 3.2.2
macOS 12.1: Lazarus 2.2.6 (64 bit Cocoa M1)
Ubuntu 18.04.3: Lazarus 2.2.6 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.2.6 (64 bit on VBox)

BobDog

  • Sr. Member
  • ****
  • Posts: 394
Re: GetURLText
« Reply #20 on: March 06, 2023, 09:02:28 pm »
Windows method, takes a couple of seconds to find site.

Code: Pascal  [Select][+][-]
  1.  
  2.  
  3. {$APPTYPE CONSOLE}
  4. uses
  5. sysutils,process;
  6.  
  7.  
  8. function download(site:ansistring):ansistring;
  9. var
  10. c:ansistring='';
  11. newfile:ansistring;
  12. Fin:file of byte;
  13. len:int32;
  14. begin  
  15.    newfile:=GetCurrentDir +'/site.txt';
  16.  runcommand('powershell',['Invoke-WebRequest',site,'-OutFile',newfile],c);
  17.    Assign (Fin,newfile);
  18.    Reset (Fin);
  19.    len:=filesize(Fin);
  20.    setlength(c,len);
  21.    Reset (Fin,len);
  22.    BlockRead (Fin,c[1],1);
  23.    close(Fin);
  24.    deletefile(newfile);
  25.    exit(c);
  26. end;
  27.  
  28.   var
  29.   site:ansistring='https://www.example.com';
  30.   c:ansistring;
  31.   begin
  32.   c:=download(site);
  33.   writeln(c);
  34.   writeln('PRESS RETURN TO END ...');
  35.   readln;
  36.   end.
  37.  
  38.  
« Last Edit: March 07, 2023, 01:01:46 pm by BobDog »

dsiders

  • Hero Member
  • *****
  • Posts: 1084
Re: GetURLText
« Reply #21 on: March 06, 2023, 10:27:52 pm »
If you're using 64-bit, the file names are:

 libcrypto-1_1-x64.dll
libssl-1_1-x64.dll

If you're using 32-bit, the file names are:

 libcrypto-1_1.dll
libssl-1_1.dll

If your URL is valid and your uses clause has fphtttpclient and opensslsockets, that is all you need.


 
Preview Lazarus 3.99 documentation at: https://dsiders.gitlab.io/lazdocsnext

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2079
  • Fifty shades of code.
    • Delphi & FreePascal
Re: GetURLText
« Reply #22 on: March 07, 2023, 12:20:58 am »
the file names are
for completeness:
Code: Pascal  [Select][+][-]
  1.   {$IFDEF WINDOWS}
  2.   DLLSSLName: string = 'ssleay32.dll';
  3.   DLLSSLName2: string = 'libssl32.dll';
  4.   DLLSSLName3: string = {$IFDEF WIN64}'libssl-1_1-x64.dll'{$ELSE}'libssl-1_1.dll'{$ENDIF};
  5.   DLLUtilName: string = 'libeay32.dll';
  6.   DLLUtilName2: string = {$IFDEF WIN64}'libcrypto-1_1-x64.dll'{$ELSE}'libcrypto-1_1.dll'{$ENDIF};
  7.   {$ELSE}
  8.    {$IFDEF OS2}
  9.     {$IFDEF OS2GCC}
  10.   DLLSSLName: string = 'kssl10.dll';
  11.   DLLUtilName: string = 'kcrypt10.dll';
  12.   DLLSSLName2: string = 'kssl.dll';
  13.   DLLUtilName2: string = 'kcrypto.dll';
  14.     {$ELSE OS2GCC}
  15.   DLLSSLName: string = 'emssl10.dll';
  16.   DLLUtilName: string = 'emcrpt10.dll';
  17.   DLLSSLName2: string = 'ssl.dll';
  18.   DLLUtilName2: string = 'crypto.dll';
  19.     {$ENDIF OS2GCC}
  20.    {$ELSE OS2}
  21.   DLLSSLName: string = 'libssl';
  22.   DLLUtilName: string = 'libcrypto';
  23.  
  24.   { ADD NEW ONES WHEN THEY APPEAR!
  25.     Always make .so/dylib first, then versions, in descending order!
  26.     Add "." .before the version, first is always just "" }
  27.   DLLVersions: array[1..19] of string = ('', '.1.1', '.11', '.10', '.1.0.6', '.1.0.5', '.1.0.4', '.1.0.3',
  28.                                         '.1.0.2', '.1.0.1','.1.0.0','.0.9.8',
  29.                                         '.0.9.7', '.0.9.6', '.0.9.5', '.0.9.4',
  30.                                         '.0.9.3', '.0.9.2', '.0.9.1');
  31.    {$ENDIF OS2}
  32.   {$ENDIF WINDOWS}
opensslsockets -> openssl
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

 

TinyPortal © 2005-2018