THTTPClient blocks after a few consecutive requests

  

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:

I create a new THTTPClient.
I do a Post request.
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;

Comments are closed.