Recent

Author Topic: TFPHTTPClient - frequent calls raise error  (Read 308 times)

ertank

  • Sr. Member
  • ****
  • Posts: 258
TFPHTTPClient - frequent calls raise error
« on: December 12, 2019, 04:30:47 pm »
Hello,

I am using Lazarus 2.0.7, SVN rev: 62300 from fixes 2.0 branch. fpc version is 3.2.0 SVN rev: 43667 from fixes 3.2 branch. OS is Raspbian, Debian 10.2 with all updates installed.

I have below function that I frequently call.
Code: [Select]
function CallRestService(const URL: string; const Data: RawUTF8; out Response: string): Boolean;
var
  Http: TFPHTTPClient;
  MS: TMemoryStream;
  ServerResponse: TMemoryStream;
  SS: TStringStream;
begin
  Log('REST: Calling URL: ' + URL);
  Log('REST Request: ' + string(Data));
  Http := nil;
  MS := nil;
  ServerResponse := nil;
  SS := nil;
  try
    MS := TMemoryStream.Create();
    MS.Write(Pointer(Data)^, Length(Data));
    MS.Position := 0;
    ServerResponse := TMemoryStream.Create();

    Http := TFPHTTPClient.Create(nil);
    Http.RequestHeaders.Values['Content-Type'] := 'application/json';
    Http.RequestHeaders.Values['UserAgent'] := EmptyStr;
    Http.ConnectTimeout := 60 * 1000;
    Http.IOTimeout := 60 * 1000;
    if UseProxy then
    begin
      Http.Proxy.Host := 'localhost';
      Http.Proxy.Port := 8888;
    end
    else
    begin
      Http.Proxy.Host := EmptyStr;
      Http.Proxy.Port := 0;
    end;

    try
      Http.RequestBody := MS;
      Http.Post(URL, ServerResponse);  // <--- EXCEPTION HERE
    except
      on E: Exception do
      begin
        Log('CallRestService()-Http.Post(): ' + StringReplace(E.Message, sLineBreak, ' ', [rfReplaceAll]));
        Exit(False);
      end;
    end;

    SS := TStringStream.Create(EmptyStr, TEncoding.UTF8);
    ServerResponse.Position := 0;
    SS.LoadFromStream(ServerResponse);
    Response := SS.DataString;
    Log('REST Response: ' + Response);
    Result := True;
  finally
    SS.Free();
    ServerResponse.Free();
    MS.Free();
    Http.Free();
  end;
end;

When application is running without debug mode, my second call to it raises exception "Project raised exception class 'ESocketError' with message: Connect to <domain>:443 failed". My call times in log is as following:
1- 2019-12-12 18:14:14.874
2- 2019-12-12 18:14:16.540


However, if I am to run application in debug mode, put a break point on marked line in above code and hit F9 when code stops then everything goes smooth. No exception, no error. My function call times in debug run are as following:
1- 2019-12-12 18:23:41.796
2- 2019-12-12 18:23:47.452
3- 2019-12-12 18:23:51.208
4- 2019-12-12 18:23:53.944
5- 2019-12-12 18:23:56.544
6- 2019-12-12 18:23:58.496
7- 2019-12-12 18:24:00.911

All calls are to same exact domain from first to last. All calls are using HTTPS protocol.

I failed to see what I am doing wrong and searching for help, please.

Thanks & regards,
Ertan

trev

  • Sr. Member
  • ****
  • Posts: 402
  • Former Delphi 7 and Delphi 10.2 User
Re: TFPHTTPClient - frequent calls raise error
« Reply #1 on: December 13, 2019, 12:52:20 pm »
I had a similar issue a few years ago in a Delphi program. To cut a long story short, the problem was being caused by the server's firewall which was falsely detecting the quick succession of connects as a denial of service attack. I only discovered the true cause because I had access to the admin of the server in question. I believe the firewall in question was Checkpoint.

Your  "ESocketError with message: Connect to <domain>:443 failed" looked suspiciously similar to my experience at the time.

Just a thought.
o Lazarus v2.1.0 r62452, FPC v3.3.1 r42644, macOS 10.14.6 (with sup update), Xcode 10.3
o Lazarus v2.1.0 r61574, FPC v3.3.1 r42318, FreeBSD 12.0 (Parallels VM)
o Lazarus v2.1.0 r61574, FPC v3.0.4, Ubuntu 18.04 (Parallels VM)

ertank

  • Sr. Member
  • ****
  • Posts: 258
Re: TFPHTTPClient - frequent calls raise error
« Reply #2 on: December 13, 2019, 01:30:32 pm »
I am also suspecting about DDOS protection of that VPS I am connecting.
Trying to figure...