2

I'm using THTTPClient in Delphi to develop an Android app.

I have a big problem: after a few requests, when I do the next request it blocks like if there was a dead lock inside the THTTPClient's Post function.

That's how I wake a request:

  1. I create a new THTTPClient.
  2. I do a Post request.
  3. I DisposeOf the HTTPClient.

How can I solve this problem? I'm going crazy!

This is the code I'm using to make a POST request:

I call it from a loop and after a few request, it blocks on the line Request := HTTPClient.Post(URL, InStream, OutStream);.

I noticed that when if I do request to different domains, it blocks earlier than what I do many requests to the same domain.

function Post(URL: string; Data: RawByteString; var StatusCode: Integer): RawByteString;
var
  HTTPClient: THTTPClient;
  Request: IHTTPResponse;
  InStream, OutStream: TMemoryStream;
begin
  Try
    StatusCode := -1;
    HTTPClient := THTTPClient.Create;
    with HTTPClient do
    begin
      ConnectionTimeout := 10000;
      ResponseTimeout := 10000;
      UserAgent := '...';
    end;
    InStream := TMemoryStream.Create;
    OutStream := TMemoryStream.Create;
    InStream.WriteBuffer(Data[0], Length(Data));
    InStream.Seek(0, soBeginning);
    Request := HTTPClient.Post(URL, InStream, OutStream);
    StatusCode := Request.StatusCode;
    if (StatusCode >= 200) and (StatusCode <= 299) then
    begin
      SetLength(Result, OutStream.Size);
      Move(OutStream.Memory^, Result[0], OutStream.Size);
    end;
  Finally
    InStream.Free;
    OutStream.Free;
    HTTPClient.Free;
  End;
end;
Alfio
  • 21
  • 2
  • 1
    You need to provide a [mcve] – Dave Nottage Jul 25 '19 at 02:48
  • Is it possible you are POSTing requests to a server that is owned by someone other than you? Who might be detecting your rapid post rate as an attempt to scrape data from a website that does not want it scraped? And are possibly denying you a response after you trigger some abuse threshold? Or did you test this on a server under your control where you know this isn't happening? Checking the server logs would be one place to start. Also using debug DCUs and breaking into the Indy source would allow you to pinpoint exactly where the lockup is happening. – J... Jul 25 '19 at 15:27
  • Also, your try/finally is a bit wrong. In this case the easiest option is to initialize `HTTPClient`, `InStream`, and `OutStream` to `nil` before entering the `try`. This way you won't try to free an invalid pointer in `Finally` if an exception is thrown before the objects are created. – J... Jul 25 '19 at 15:29
  • I tried to change THTTPClient with TIdHTTP (Indy) and everything works fine. The problem is that Indy doesn't support SSL on Android >= 6. How can I do? – Alfio Jul 25 '19 at 19:27
  • 1
    @Alfio https://stackoverflow.com/a/37122728/3164070 – Dave Nottage Jul 25 '19 at 21:32
  • @DaveNottage it seems that it doesn't work starting from Android 7 because you can't use dynamic libraries anymore. :-( – Alfio Jul 25 '19 at 22:37
  • I've been using it on Android 7+ just fine – Dave Nottage Jul 25 '19 at 22:56
  • I’ve been using on Android as well - [see here](https://stackoverflow.com/questions/56601509/loading-openssl-dynamic-libraries-arm-x86-fmx-c/56606686#56606686) – relayman357 Jul 26 '19 at 04:01

0 Answers0