Forum > Networking and Web Programming
TFPHttpClient timeout error
Leledumbo:
--- Quote from: jhorta on November 25, 2024, 09:40:16 pm ---Any tips? Could I be missing something?
--- End quote ---
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 [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---{$mode objfpc}{$H+} uses Classes,SysUtils,fphttpclient, fpjson, opensslsockets; Function Send(Url : String) : String;Var Client: TFPHttpClient; Response: TStringStream;Begin Client := TFPHttpClient.Create(nil); Client.ConnectTimeout := 6000; Client.AddHeader('Content-Type', 'application/json'); Client.AddHeader('Accept', 'application/json'); Client.RequestBody := TRawByteStringStream.Create('{"jwt": "", "codAplicacao": "F60EB930-9B1B-11EF-9657-02240D86274B"}'); Response := TStringStream.Create(''); Try Try Client.Post(Url, Response); Result := 'Response: ' + Response.DataString; Except on E: Exception do Begin Result := 'Something wrong: ' + E.Message; End; End; Finally Client.RequestBody.Free; Client.Free; Response.Free; End;End; begin WriteLn(Send('http://localhost:4000/local/inicio/hortech'));end. with the corresponding server:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---{$mode objfpc}{$H+} uses httpdefs, httproute, fpjson, jsonparser, fphttpapp; procedure whatever(AReq: TRequest; AResp: TResponse);var LJSONReqBody: TJSONObject; LCodAplicacao: TJSONData;begin LJSONReqBody := TJSONObject(GetJSON(AReq.Content)); LCodAplicacao := LJSONReqBody.FindPath('codAplicacao'); if Assigned(LCodAplicacao) then AResp.Content := LCodAplicacao.AsString else AResp.Content := 'codAplicacao not supplied';end; begin HTTPRouter.RegisterRoute('*', @whatever); Application.Initialize; Application.Port := 4000; Application.Run;end. 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:
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 [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---unit Unit1; {$mode objfpc}{$H+} interface uses Classes, SysUtils, Forms, Controls, Graphics, Dialogs,ExtCtrls,StdCtrls, fphttpclient, opensslsockets, openssl; type { TForm1 } TForm1 = class(TForm) Button1:TButton; Memo1:TMemo; Panel1:TPanel; procedure Button1Click(Sender:TObject); private const token ='NjkxNThkZTAtYjIwNC00NzQ4LWJlYjMtYWIwN2UyMjViZTIw'; const sernum='1423003482'; const IPaddr='192.168.0.180:443'; public end; var Form1: TForm1; implementation {$R *.lfm} { TForm1 } procedure TForm1.Button1Click(Sender:TObject);var Client: TFPHttpClient; Response: TStringStream;begin Client := TFPHttpClient.Create(nil); Client.AddHeader('User-Agent', 'Mozilla/5.0 (compatible; fpweb)'); //Client.AddHeader('Content-Type', 'application/json; charset=UTF-8'); //Client.AddHeader('Accept', 'application/json'); //Client.AddHeader('Authorization','Basic '+token); Client.AllowRedirect := true; Response := TStringStream.Create(''); try try //Client.Post('https://'+IPaddr+'/public/api/v3.0/stations/'+sernum+'/channels/1/access/qrcode.data', Response); Memo1.Lines.Add(Client.Get('https://www.bing.com'));// Memo1.Lines.Add('Response Code: ' + IntToStr(Client.ResponseStatusCode)); // better be 200 except on E: Exception do Memo1.Lines.Add(E.Message); end; finally Client.Free; Response.Free; end;end; end. Got this from here.
two_oceans:
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:
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 [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---{$mode delphi}{$ifdef windows}{$apptype console}{$endif} uses fphttpclient, openssl, opensslsockets; var Client: TFPHttpClient; begin Client := TFPHttpClient.Create(nil); try Client.AddHeader('User-Agent', 'Mozilla/5.0 (compatible; fpweb)'); { Allow redirections } Client.AllowRedirect := true; writeln(Client.Get('https://google.com/')); finally Client.Free; end;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 [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program clienttest;{$mode delphi}{$ifdef windows}{$apptype console}{$endif} uses {$IFDEF UNIX} cthreads, {$ENDIF} Interfaces,lclintf, sysutils,classes,fphttpclient,openssl,opensslsockets; var Client: TFPHttpClient; List:TStringList;begin Client := TFPHttpClient.Create(nil); List := TStringlist.Create; try Client.AddHeader('User-Agent', 'Mozilla/5.0 (compatible; fpweb)'); { Allow redirections } Client.AllowRedirect := true; List.Text:=Client.Get('https://google.com/'); List.SaveToFile('c:\users\public\atest.html'); // change paths for Linux. './atest.html'; OpenURL('C:\users\public\atest.html'); finally List.Free; Client.Free; end;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.
Navigation
[0] Message Index
[*] Previous page