Recent

Author Topic: Downloading form HTTPS  (Read 1485 times)

jts

  • Newbie
  • Posts: 2
Downloading form HTTPS
« on: June 04, 2021, 04:20:48 pm »
Hello,
Thats my first post, but I did some research before posting.

I need function (or procedure, whatever).

I installed synapse, and INDY 10, I'm using Delphi 7.

Searched here and there and I modyfied this code posts: click

There's no problem whet url is just standard HTTP, but when its HTTPS it give 500 code.
I add ssl_openssl, ssl_openssl_lib to uses, as was adviced at some thread at this forum. (Can't find it right now)


Can anyone help me out and modify this code for HTTPS to works?


Code: [Select]
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, httpsend, IdHTTP, IdStack, ssl_openssl, ssl_openssl_lib ;

function pobierzzsieci(URL: string; FileName: string): boolean;
var
  httpClient: THTTPSend;
  noErrors: boolean;
  Path: string;
begin
  noErrors:= True;
  httpClient := THTTPSend.Create;

  if httpClient.HTTPMethod('GET', URL) then
  begin
    // for windows
    FileName := StringReplace(FileName, '/', '\',[rfReplaceAll, rfIgnoreCase]);

    Path := IncludeTrailingPathDelimiter(GetCurrentDir) + FileName;



    if httpClient.ResultCode = 200 then
      begin
        if not DirectoryExists(ExtractFilePath(Path)) then  ForceDirectories(ExtractFilePath(Path));
        httpClient.Document.SaveToFile(path);
      end
    else
        noErrors := False;

    end
    else

    begin
      showmessage('something wrong code other than 200');
      noErrors := False;
    end;
  Result := noErrors;
  //Result:=httpClient.ResultCode ;
  httpClient.Free;
end;

And
Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);
var
url3: string;
plik: string;

begin

plik:='aaa\nowy\testowy\plik001.jpg';
url3:='https://kurdubelski.pl/images/avatar.jpg';



 if pobierzzsieci(url3,plik) then  label1.Caption:='pobrano '+url
 else showmessage('theres no such file');

                                           
end;

paweld

  • Sr. Member
  • ****
  • Posts: 292
Re: Downloading form HTTPS
« Reply #1 on: June 04, 2021, 05:30:43 pm »
there must be files in the application directory (or Windows directory):
- ssleay32.dll
- libeay32.dll

https://github.com/IndySockets/OpenSSL-Binaries
Best regards
paweld

jts

  • Newbie
  • Posts: 2
Re: Downloading form HTTPS
« Reply #2 on: June 04, 2021, 05:59:21 pm »
I downloaded and copy to (both) Windows and programm directories.

Nothing changed.

EDIT:
It worked.
Just need to copy to System 32 AND to SysWOT64


thanks.
« Last Edit: June 04, 2021, 06:13:05 pm by jts »

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1528
  • Former Delphi 1-7, 10.2 user
Re: Downloading form HTTPS
« Reply #3 on: June 07, 2021, 10:54:49 am »
Alternatively, forget about OpenSSL DLLs and use Window's built-in routines:

Code: Pascal  [Select][+][-]
  1. {$IFDEF WINDOWS}
  2. // Need to use Windows WinInet to avoid issue with HTTPS
  3. // needing two OpenSSL DLLs to be provided with application
  4. // if using TFPHttpClient.
  5. // The WinINet API also gets any connection and proxy settings
  6. // set by Internet Explorer. Blessing or curse?
  7.  
  8. function GetWebPage(const Url: string): string;
  9. var
  10.   NetHandle: HINTERNET;
  11.   UrlHandle: HINTERNET;
  12.   Buffer: array[0..1023] of Byte;
  13.   BytesRead: dWord;
  14.   StrBuffer: UTF8String;
  15. begin
  16.   Result := '';
  17.   BytesRead := Default(dWord);
  18.   NetHandle := InternetOpen('Mozilla/5.0(compatible; WinInet)', INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
  19.  
  20.   // NetHandle valid?
  21.   if Assigned(NetHandle) then
  22.     Try
  23.       UrlHandle := InternetOpenUrl(NetHandle, PChar(Url), nil, 0, INTERNET_FLAG_RELOAD, 0);
  24.  
  25.       // UrlHandle valid?
  26.       if Assigned(UrlHandle) then
  27.         Try
  28.           repeat
  29.             InternetReadFile(UrlHandle, @Buffer, SizeOf(Buffer), BytesRead);
  30.             SetString(StrBuffer, PAnsiChar(@Buffer[0]), BytesRead);
  31.             Result := Result + StrBuffer;
  32.           until BytesRead = 0;
  33.         Finally
  34.           InternetCloseHandle(UrlHandle);
  35.         end
  36.       // o/w UrlHandle invalid
  37.       else
  38.         ShowMessage('Cannot open URL: ' + Url);
  39.     Finally
  40.       InternetCloseHandle(NetHandle);
  41.     end
  42.   // NetHandle invalid
  43.   else
  44.     raise Exception.Create('Unable to initialize WinInet');
  45. end;
  46. {$ENDIF}
Lazarus 2.1 r65061 FPC 3.3.1 r49223 macOS 10.14.6 Xcode 11.3.1
Lazarus 2.3 r65500 FPC 3.3.1 r49634 macOS 11.5.1 aarch64 Xcode 12.5.1
Lazarus 2.1 r61574 FPC 3.3.1 r42318 FreeBSD 12.1 amd64 VMware VM
Lazarus 2.1 r61574 FPC 3.0.4 Ubuntu 20.04 Parallels VM
Lazarus 2.0.10 FPC 3.2.0 Win10 Parallels VM

BeniBela

  • Hero Member
  • *****
  • Posts: 807
    • homepage
Re: Downloading form HTTPS
« Reply #4 on: June 14, 2021, 04:24:15 pm »
Or with my internet tools to use these Window's  routines on Windows and Synapse/OpenSSL everywhere else. Then it needs no DLLs there, but is still portable

Code: [Select]
uses simpleinternet;

retrieve('https://kurdubelski.pl/images/avatar.jpg')

 

TinyPortal © 2005-2018