Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get images from HTTPS URL in Delphi?

I'm trying to load a TImage starting from an URL as explained in this answer.

uses
  GIFImg;

procedure TForm1.Button1Click(Sender: TObject);
var
  Stream : TMemoryStream;
  GIF : TGIFImage;
begin
  Stream := TMemoryStream.Create;
  GIF := TGIFImage.Create;
  try
    IdHTTP1.Get('http://www.google.com/intl/en_ALL/images/logo.gif', Stream);
    //IdHTTP1.Get('https://www.google.com/intl/en_ALL/images/logo.gif', Stream);

    Stream.Position := 0;
    GIF.LoadFromStream(Stream);

    Image1.Picture.Assign(GIF);

  finally
    FreeAndNil(GIF);
    FreeAndNil(Stream);
  end;
end;

All works good if the URL starts with a simple HTTP.

When I try to load from an HTTPS URL, I get an EIdIOHandlerPropInvalid exception with message:

IOHandler value is not valid.

I've tried adding a TIdSSLIOHandlerSocketOpenSSL and setting it as IOHandler for the TIdHTTP component.

enter image description here enter image description here

After doing that, I get an EIdOSSLCouldNotLoadSSLLibrary exception with the following message:

Could not load SSL library.

Is there something wrong in the properties or some other problem?

like image 579
Fabrizio Avatar asked Aug 13 '26 06:08

Fabrizio


2 Answers

You need the SSL library (two .DLL files) either installed in your Windows, or distributed along with your application.

Seems to be downloadable here:

https://slproweb.com/products/Win32OpenSSL.html

Or via their official web site (but I can't find downloadable binaries there - only source code):

https://www.openssl.org/source/

Pre-compiled .DLLs for use with Indy can be downloaded here:

https://indy.fulgan.com/SSL/ (Thanks, Zed)

like image 178
HeartWare Avatar answered Aug 15 '26 09:08

HeartWare


When I try to load from an HTTPS URL, I get an EIdIOHandlerPropInvalid exception with message:

IOHandler value is not valid.

That error means you don't have an TIdSSLIOHandlerSocketBase-derived component attached to the TIdHTTP.IOHandler property, such as TIdSSLIOHandlerSocketOpenSSL.

This error also tells me that you are using an outdated version of Indy and need to upgrade. TIdHTTP automatically creates a default SSLIOHandler when requesting an HTTPS url and no IOHandler is currently assigned, and has done so for a long time.

I've tried adding a TIdSSLIOHandlerSocketOpenSSL and setting it as IOHandler for the TIdHTTP component.

That is the correct solution, if you do not want to upgrade, or if you need to customize the SSLIOHandler settings, such as for enabling TLS 1.1+ (which TIdSSLIOHandlerSocketOpenSSL does not do by default yet).

After doing that, I get an EIdOSSLCouldNotLoadSSLLibrary exception with the following message:

Could not load SSL library.

That error means that either

  • you have not deployed the OpenSSL DLLs (libeay32.dll and ssleay32.dll) with your app, or at least have them installed somewhere on your system's DLL search path.

  • you are using a version of the DLLs that is incompatible with your version of Indy. The current version of Indy supports OpenSSL 1.0.2 and earlier (support for OpenSSL 1.1.x is currently a work in progress).

After the error is raised, you can use Indy's WhichFailedToLoad() function in the IdSSLOpenSSLHeaders unit to determine the root cause of the failure - whether the DLLs themselves could not be loaded in memory (because they were not found, etc), or because they are missing specific exports that Indy requires.

like image 31
Remy Lebeau Avatar answered Aug 15 '26 07:08

Remy Lebeau



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!