Recent

Author Topic: TFPHttpClient timeout error  (Read 7424 times)

jhorta

  • New member
  • *
  • Posts: 7
TFPHttpClient timeout error
« on: November 25, 2024, 09:40:16 pm »
Im trying to connect and post to an api running on my local machine using TFPHttpClient but im getting a timeout error. Everything Works fine when i call via Postman.

I tried to use the ConnectTimeout property(60) but I kept getting the same error.

Any tips? Could I be missing something?

Running on Windows 10, Lazarus 3.4

Error : Connection to localhost:4000 timed out.

My code:

Code: Pascal  [Select][+][-]
  1. Procedure Send;
  2. Var Client: TFPHttpClient;
  3.     Response: TStringStream;
  4. Begin
  5.    Client := TFPHttpClient.Create(nil);
  6.    Client.AddHeader('Content-Type', 'application/json; charset=UTF-8');
  7.    Client.RequestBody := TRawByteStringStream.Create('{"jwt": "", ' + '"codAplicacao": "F60EB930-9B1B-11EF-9657-02240D86274B"}');
  8.  
  9.    Response := TStringStream.Create('');
  10.  
  11.    Try
  12.       Try
  13.          Client.Post('http://localhost:4000/local/inicio/hortech', Response);
  14.          GeraLog('Response: ' + String(Response));
  15.        Except on E: Exception do
  16.           Begin
  17.              GeraLog('Something wrong: ' + E.Message);
  18.           End;
  19.        End;
  20.    Finally
  21.        Client.RequestBody.Free;
  22.        Client.Free;
  23.        Response.Free;
  24.    End;
  25. End;
  26.  

jhorta

  • New member
  • *
  • Posts: 7
Re: TFPHttpClient timeout error
« Reply #1 on: November 28, 2024, 02:49:45 am »
I tried a few things but nothing worked. A bit frustrating. I should add that during some testing, I noticed that the error message occurs for any url, even if it doesn't exist or is unavailable. I appreciate any tips, links, etc.

paweld

  • Hero Member
  • *****
  • Posts: 1494
Re: TFPHttpClient timeout error
« Reply #2 on: November 28, 2024, 06:55:17 am »
The error may be due to the fact that the program is blocked on the firewall.
Overall the code is almost OK, only you have an error when get the response string - it should be:
Code: Pascal  [Select][+][-]
  1. GeraLog('Response: ' + Response.DataString);
instead of:
Code: Pascal  [Select][+][-]
  1. GeraLog('Response: ' + String(Response));
Best regards / Pozdrawiam
paweld

Thaddy

  • Hero Member
  • *****
  • Posts: 18328
  • Here stood a man who saw the Elbe and jumped it.
Re: TFPHttpClient timeout error
« Reply #3 on: November 28, 2024, 07:36:57 am »
ConnectTimeout property, I believe 60 are ms, so make it much higher, like 1000. A timeout of 60ms is useless.
« Last Edit: November 28, 2024, 07:39:02 am by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

jhorta

  • New member
  • *
  • Posts: 7
Re: TFPHttpClient timeout error
« Reply #4 on: November 28, 2024, 07:42:33 pm »
ConnectTimeout set to 1000, 2000, 3000...

permission for the program configured in the firewall

Firewall disabled

Nothing worked

TRon

  • Hero Member
  • *****
  • Posts: 4377
Re: TFPHttpClient timeout error
« Reply #5 on: November 28, 2024, 11:27:41 pm »
@jhorta:
must be your setup somewhere because when testing it times-out almost immediate for me.

There are sites to test these kind of things such as JSONPlaceholder and reqbin and I am sure there are many others.

So, your postman also post its requests insecure ?
Today is tomorrow's yesterday.

jhorta

  • New member
  • *
  • Posts: 7
Re: TFPHttpClient timeout error
« Reply #6 on: November 29, 2024, 02:56:44 pm »
must be your setup somewhere because when testing it times-out almost immediate for me.
Maybe. Does anyone know of any way to trace the api call to analyze its contents?

So, your postman also post its requests insecure ?
The firewall ask about and i give permission. After that i can see the entry in firewall panel. I added the same permission to my program(Project1.exe). It didn't work

Thaddy

  • Hero Member
  • *****
  • Posts: 18328
  • Here stood a man who saw the Elbe and jumped it.
Re: TFPHttpClient timeout error
« Reply #7 on: November 29, 2024, 05:44:09 pm »
The full program or a reproducable example...Because in the case of everyone else it works.. >:D
This is really frustrating. I am an old man with time to test..for free..
« Last Edit: November 29, 2024, 05:46:42 pm by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

jhorta

  • New member
  • *
  • Posts: 7
Re: TFPHttpClient timeout error
« Reply #8 on: November 29, 2024, 08:07:47 pm »
Here is the complete code, in case you ever have that "philanthropic" moment.

The interface consists of an edit button to show the result and two buttons, one for local access and another to access the same application in the cloud (this one has an SSL problem, which is in another topic on this forum).

The application is an AWS lambda, developed in Python, running locally using the Serverless Framework. I even thought that this could be the problem, but it works normally via Postman.

As you said, it is really frustrating.

To avoid being stuck indefinitely, since I already have the entire backend built, I decided to download Delphi Community. It took me a while to find the correct syntax for TRESTClient, but everything worked well, locally and in the cloud. That gives me a year to try to solve it in FPC.

Cheers.


Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, fphttpclient, fpjson, opensslsockets;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Button1: TButton;
  16.     Button2: TButton;
  17.     Edit1: TEdit;
  18.     procedure Button1Click(Sender: TObject);
  19.     procedure Button2Click(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. Function Send(Url : String) : String;
  36. Var Client: TFPHttpClient;
  37.     Response: TStringStream;
  38. Begin
  39.    Client := TFPHttpClient.Create(nil);
  40.    Client.ConnectTimeout := 6000;
  41.    Client.AddHeader('Content-Type', 'application/json');
  42.    Client.AddHeader('Accept', 'application/json');
  43.    Client.RequestBody := TRawByteStringStream.Create('{"jwt": "", "codAplicacao": "F60EB930-9B1B-11EF-9657-02240D86274B"}');
  44.  
  45.    Response := TStringStream.Create('');
  46.  
  47.    Try
  48.       Try
  49.          Client.Post(Url, Response);
  50.          Result := 'Response: ' + Response.DataString;
  51.        Except on E: Exception do
  52.           Begin
  53.              Result := 'Something wrong: ' + E.Message;
  54.           End;
  55.        End;
  56.    Finally
  57.        Client.RequestBody.Free;
  58.        Client.Free;
  59.        Response.Free;
  60.    End;
  61. End;
  62.  
  63. procedure TForm1.Button1Click(Sender: TObject);
  64. begin
  65.    Edit1.Text := Send('https://mystack.execute-api.us-east-2.amazonaws.com/dev/inicio/hortech');
  66. end;
  67.  
  68. procedure TForm1.Button2Click(Sender: TObject);
  69. begin
  70.    Edit1.Text := Send('http://localhost:4000/local/inicio/hortech');
  71. end;
  72.  
  73. end.
  74.  

Thaddy

  • Hero Member
  • *****
  • Posts: 18328
  • Here stood a man who saw the Elbe and jumped it.
Re: TFPHttpClient timeout error
« Reply #9 on: November 29, 2024, 10:01:19 pm »
If you do not run a server on localhost, of course you can not test your code.....

That is after testing my conclusion: there is no server.
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8831
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: TFPHttpClient timeout error
« Reply #10 on: December 07, 2024, 04:56:00 pm »
Any tips? Could I be missing something?
Without knowing exactly what your server side expects, it's a guessing game. I don't like guessing game. But, your client code is fine. Here I extract it to a simple compilable runnable program:
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}{$H+}
  2.  
  3. uses Classes,SysUtils,fphttpclient, fpjson, opensslsockets;
  4.  
  5. Function Send(Url : String) : String;
  6. Var Client: TFPHttpClient;
  7.     Response: TStringStream;
  8. Begin
  9.    Client := TFPHttpClient.Create(nil);
  10.    Client.ConnectTimeout := 6000;
  11.    Client.AddHeader('Content-Type', 'application/json');
  12.    Client.AddHeader('Accept', 'application/json');
  13.    Client.RequestBody := TRawByteStringStream.Create('{"jwt": "", "codAplicacao": "F60EB930-9B1B-11EF-9657-02240D86274B"}');
  14.  
  15.    Response := TStringStream.Create('');
  16.  
  17.    Try
  18.       Try
  19.          Client.Post(Url, Response);
  20.          Result := 'Response: ' + Response.DataString;
  21.        Except on E: Exception do
  22.           Begin
  23.              Result := 'Something wrong: ' + E.Message;
  24.           End;
  25.        End;
  26.    Finally
  27.        Client.RequestBody.Free;
  28.        Client.Free;
  29.        Response.Free;
  30.    End;
  31. End;
  32.  
  33. begin
  34.   WriteLn(Send('http://localhost:4000/local/inicio/hortech'));
  35. end.
  36.  
with the corresponding server:
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}{$H+}
  2.  
  3. uses
  4.     httpdefs,
  5.     httproute,
  6.     fpjson,
  7.     jsonparser,
  8.     fphttpapp;
  9.  
  10. procedure whatever(AReq: TRequest; AResp: TResponse);
  11. var
  12.   LJSONReqBody: TJSONObject;
  13.   LCodAplicacao: TJSONData;
  14. begin
  15.   LJSONReqBody := TJSONObject(GetJSON(AReq.Content));
  16.   LCodAplicacao := LJSONReqBody.FindPath('codAplicacao');
  17.   if Assigned(LCodAplicacao) then
  18.     AResp.Content := LCodAplicacao.AsString
  19.   else
  20.     AResp.Content := 'codAplicacao not supplied';
  21. end;
  22.  
  23. begin
  24.    HTTPRouter.RegisterRoute('*', @whatever);
  25.    Application.Initialize;
  26.    Application.Port := 4000;
  27.    Application.Run;
  28. end.
  29.  
This is the best that I can do given everything you've informed. You need to check whether the Python server side expects certain headers. Postman does act like a real browser so it sends the usual browser headers, not only content-type and accept as you tell fphttpclient to. Some servers are typically picky when some of those headers aren't present (e.g.: user-agent, accept-encoding, connection, etc.).

geraldholdsworth

  • Sr. Member
  • ****
  • Posts: 265
Re: TFPHttpClient timeout error
« Reply #11 on: January 23, 2025, 01:11:21 pm »
Can I jump onto this post with a similar issue?
I'm getting Connect to www.bing.com:443 failed
(I'm actually trying to write some code to talk to a Sennheiser MobileConnect, but if I can't connect to Bing, what hope have I got of connecting to this device?)
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, opensslsockets, openssl;
  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.   const token ='NjkxNThkZTAtYjIwNC00NzQ4LWJlYjMtYWIwN2UyMjViZTIw';
  22.   const sernum='1423003482';
  23.   const IPaddr='192.168.0.180:443';
  24.  public
  25.  
  26.  end;
  27.  
  28. var
  29.  Form1: TForm1;
  30.  
  31. implementation
  32.  
  33. {$R *.lfm}
  34.  
  35. { TForm1 }
  36.  
  37. procedure TForm1.Button1Click(Sender:TObject);
  38. var
  39.   Client: TFPHttpClient;
  40.   Response: TStringStream;
  41. begin
  42.   Client := TFPHttpClient.Create(nil);
  43.   Client.AddHeader('User-Agent', 'Mozilla/5.0 (compatible; fpweb)');
  44.   //Client.AddHeader('Content-Type', 'application/json; charset=UTF-8');
  45.   //Client.AddHeader('Accept', 'application/json');
  46.   //Client.AddHeader('Authorization','Basic '+token);
  47.   Client.AllowRedirect := true;
  48.   Response := TStringStream.Create('');
  49.   try
  50.     try
  51.       //Client.Post('https://'+IPaddr+'/public/api/v3.0/stations/'+sernum+'/channels/1/access/qrcode.data', Response);
  52.       Memo1.Lines.Add(Client.Get('https://www.bing.com'));
  53. //      Memo1.Lines.Add('Response Code: ' + IntToStr(Client.ResponseStatusCode)); // better be 200
  54.     except on E: Exception do
  55.       Memo1.Lines.Add(E.Message);
  56.     end;
  57.   finally
  58.     Client.Free;
  59.     Response.Free;
  60.   end;
  61. end;
  62.  
  63. end.
  64.  
Got this from here.

two_oceans

  • Newbie
  • Posts: 1
Re: TFPHttpClient timeout error
« Reply #12 on: July 15, 2025, 04:48:47 am »
Hello. I'd like suggest possible reason of problem in first message of topic. Applicable if program can't access to target port in range ~4000-7000 on Windows server. Some mechanics keep this ports locked. It's not Windows firewall, something else.

For me it was Windows Server 2016 and port 6033 (like mirror of standart mysql port 3306). No program could access mysql service on this port (localhost or 127.0.0.1 or 192.168.x.x address), but it perfectly worked when I changed port of service and port in program to 633. So when I see topic starter mention port 4000 I remember this case immediately.

I believe previous message about bing and port 433 is not simular issue and misplaced in this topic.

Thaddy

  • Hero Member
  • *****
  • Posts: 18328
  • Here stood a man who saw the Elbe and jumped it.
Re: TFPHttpClient timeout error
« Reply #13 on: July 15, 2025, 05:52:22 am »
TfpHttpclient and the way openssl is handled have changed considerably since fpc 2.7.1
The example in the wiki can be reduced to:
Code: Pascal  [Select][+][-]
  1. {$mode delphi}{$ifdef windows}{$apptype console}{$endif}
  2.  
  3. uses
  4.   fphttpclient,
  5.   openssl,
  6.   opensslsockets;
  7.  
  8. var
  9.   Client: TFPHttpClient;
  10.  
  11. begin
  12.   Client := TFPHttpClient.Create(nil);
  13.   try
  14.     Client.AddHeader('User-Agent', 'Mozilla/5.0 (compatible; fpweb)');
  15.     { Allow redirections }
  16.     Client.AllowRedirect := true;
  17.     writeln(Client.Get('https://google.com/'));
  18.   finally
  19.     Client.Free;
  20.   end;
  21. end.
Tested.
OpenSSL initialization is automatic in 3.2.0 or higher. FPC 3.3.1 can use openssl 3.
Ergo: the wiki needs an overhaul, it is not current.

Lazarus version, add lcl to your project dependencies.:
Code: Pascal  [Select][+][-]
  1. program clienttest;
  2. {$mode delphi}{$ifdef windows}{$apptype console}{$endif}
  3.  
  4. uses
  5.   {$IFDEF UNIX}
  6.   cthreads,
  7.   {$ENDIF}
  8.   Interfaces,lclintf, sysutils,classes,fphttpclient,openssl,opensslsockets;
  9.  
  10. var
  11.   Client: TFPHttpClient;
  12.   List:TStringList;
  13. begin
  14.   Client := TFPHttpClient.Create(nil);
  15.   List := TStringlist.Create;
  16.   try
  17.     Client.AddHeader('User-Agent', 'Mozilla/5.0 (compatible; fpweb)');
  18.     { Allow redirections }
  19.     Client.AllowRedirect := true;
  20.     List.Text:=Client.Get('https://google.com/');
  21.     List.SaveToFile('c:\users\public\atest.html'); // change paths for Linux. './atest.html';
  22.     OpenURL('C:\users\public\atest.html');
  23.   finally
  24.     List.Free;
  25.     Client.Free;
  26.   end;
  27. end.

If there are still problems with the connection to your server, let me know: I do not have such a device, so I basically need to know what protocol it uses and what port. At least now you have client code that is known to work.
I have many Raspberry's that run server code that can connect in the same way as above.
Note this code requires a recent OpenSSL and I used OpenSSL 3.
If it works against google, you can be assured there is no communications problem at the client side.

Plz use a port above 1000, though as per IANA documentation. Port 633 is reserved for service status updates.
Your original 4000 should be OK.

« Last Edit: July 15, 2025, 07:27:02 am by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

 

TinyPortal © 2005-2018