Recent

Author Topic: FPHTTPClient and SSL not working  (Read 449 times)

geraldholdsworth

  • Full Member
  • ***
  • Posts: 222
FPHTTPClient and SSL not working
« on: January 23, 2025, 08:44:33 pm »
Hi all,

Following on from my post here, I've worked out that it is just HTTPS URLs that fail. However, https://google.com works (as does google.co.uk). I get, for Bing for example, is Connect to www.bing.com:443 failed.
I have absolutely no idea how to find out what version of FPHTTPClient, or OpenSSL, I am using or even where to find a newer version or even how to install it.

I'm using Lazarus 3.0 on macOS 15.

The code is currently:
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.  Classes, SysUtils, Forms, Controls, Graphics, Dialogs,ExtCtrls,StdCtrls,
  9.  fphttpclient, openssl, opensslsockets;
  10.  
  11. type
  12.  
  13.  { TForm1 }
  14.  
  15.  TForm1 = class(TForm)
  16.   Button1:TButton;
  17.   Memo1:TMemo;
  18.   Panel1:TPanel;
  19.   procedure Button1Click(Sender:TObject);
  20.  private
  21.  
  22.  public
  23.  
  24.  end;
  25.  
  26. var
  27.  Form1: TForm1;
  28.  
  29. implementation
  30.  
  31. {$R *.lfm}
  32.  
  33. { TForm1 }
  34.  
  35. procedure TForm1.Button1Click(Sender:TObject);
  36. begin
  37.  InitSSLInterface;
  38.  With TFPHTTPClient.Create(Nil) do
  39.  begin
  40.   AllowRedirect:=True;
  41.   AddHeader('User-Agent', 'Mozilla/5.0 (compatible; fpweb)');
  42.   try
  43.    try
  44.     Memo1.Lines.Add(Get('https://www.bing.com'));
  45.    except on E: Exception do
  46.     Memo1.Lines.Add(E.Message);
  47.    end;
  48.   finally
  49.    Free;
  50.   end;
  51.  end;
  52. end;
  53.  
  54. end.
  55.  

I have seen some code using "ValidateSSLCertificate" with TFPHTTPClient, but Lazarus reports this as unknown.

Thaddy

  • Hero Member
  • *****
  • Posts: 16419
  • Censorship about opinions does not belong here.
Re: FPHTTPClient and SSL not working
« Reply #1 on: January 24, 2025, 08:21:28 am »
If you need to call InitSSLInterface then you are using a version that is at least 12 years old.
That will unfortunately limit any support from me.
I suppose your SSL libraries are outdated too?
This minimalistic code should work, well, works:
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}{$H+}
  2. uses
  3.  sysutils,
  4.  fphttpclient,
  5.  opensslsockets;
  6. var
  7.   client:TfpHttpClient;
  8. begin
  9.   client := TfpHttpClient.Create(nil);
  10.   try
  11.     try
  12.       client.AllowRedirect := true;
  13.       client.RequestHeaders.Add('User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3');
  14.       writeln(client.get('https://www.bing.com'));
  15.     except
  16.        On E:EHttpClient do
  17.           writeln (E.message)
  18.        else
  19.           raise;
  20.     end;
  21.   finally
  22.     client.free;
  23.   end;
  24. end.
If it does not work your installation is hopelessly outdated and I can not help you any further.
« Last Edit: January 24, 2025, 08:31:08 am by Thaddy »
There is nothing wrong with being blunt. At a minimum it is also honest.

TRon

  • Hero Member
  • *****
  • Posts: 3822
I do not have to remember anything anymore thanks to total-recall.

Thaddy

  • Hero Member
  • *****
  • Posts: 16419
  • Censorship about opinions does not belong here.
Re: FPHTTPClient and SSL not working
« Reply #3 on: January 24, 2025, 08:46:55 am »
Note the above code should therefor work on all platforms that support fcl-net.
The technical information in the wiki is maybe a bit over the top for simple users.
Boils simply down to: be current.
There is nothing wrong with being blunt. At a minimum it is also honest.

geraldholdsworth

  • Full Member
  • ***
  • Posts: 222
Re: FPHTTPClient and SSL not working
« Reply #4 on: January 24, 2025, 09:17:45 am »
Thank you both.

It seems, to me, that as the eventual target for the application I'm writing is likely to be either a RaspberryPi or a Windows machine, getting it working on a Mac is a fruitless endeavour...for now.

I have found that, for the unit I'm trying to communicate with, curl in Terminal also works (and it also runs on Windows command line) - so I was thinking about firing that off from a Lazarus application and somehow harnessing the output (maybe saving the output to a file which the application then picks up).
« Last Edit: January 24, 2025, 09:34:08 am by geraldholdsworth »

Thaddy

  • Hero Member
  • *****
  • Posts: 16419
  • Censorship about opinions does not belong here.
Re: FPHTTPClient and SSL not working
« Reply #5 on: January 24, 2025, 12:45:01 pm »
My code works on a raspberry pi too..(all flavors)But it also works on a mac.
« Last Edit: January 24, 2025, 12:46:40 pm by Thaddy »
There is nothing wrong with being blunt. At a minimum it is also honest.

 

TinyPortal © 2005-2018