Hello,
I'm having a play around with TFPHttpClient and TThread in order to get a better understanding of how these components work together. I used
https://forum.lazarus.freepascal.org/index.php?topic=40773.0 as a starting point although that post is a few years old and I'm aware that we should use opensslsockets instead.
So, following the post above I have a class TMyHttpRequest:
type
uses
Classes, SysUtils, fphttpclient, opensslsockets;
{ TMyHTTPRequest }
TMyHTTPRequest = class(TThread)
private
fApi: String;
fRes: String;
public
procedure Execute; override;
property api: string read fApi write fApi;
property res: string read fRes write fRes;
end;
implementation
{ TMyHTTPRequest }
procedure TMyHTTPRequest.Execute;
var
Client: TFPHTTPClient;
begin
Client := TFPHTTPClient.Create(nil);
writeln(api);
res := Client.Get(api);
writeln(res);
FreeAndNil(Client);
end;
and the idea is that the main program can create multiple instances of these and they'll go off and do their thing in separate threads.
Inevitably the calling method is a button click on a form:
procedure TForm1.Button1Click(Sender: TObject);
var
MYThread: array[0..2] of TMyHTTPRequest;
i: integer;
begin
for i:=1 to 3 do
begin
MYThread[i] := TMyHTTPRequest.Create(True);
MyThread[i].api:='https://jsonplaceholder.typicode.com/posts/'+inttostr(i);
MYThread[i].FreeOnTerminate := True;
MYThread[i].Start;
end;
end;
So, the first thread works as expected - the call is made and data is returned correctly, but the second and third result in an EInOutError 'could not initialize openssl library'.
If I put sleep(1000) in the loop, giving each thread time to complete before starting the next it works fine. So that suggests that it isn't possible to initialize openssl when it's already in use.
So I'm wondering what is the correct way to make multiple https calls on separate threads?
Thanks in anticipation.
John C