Mark Cooper Mark Cooper - 4 months ago 26
HTTP Question

How to analyze HTTP Post response faster

I have written a simple program that checks data periodically. The problem is that the checking process slows down over time. First it sends 2 posts in one second, after like 45s it sends one post, eventually it stops completely. I do not know what is wrong here.

procedure TForm2.CheckURLs;
var
lHTTP: TIdHTTP;
IdSSL : TIdSSLIOHandlerSocketOpenSSL;
N: Integer;
Access: string;
Params, Reply: TStringList;
Cookie: TIdCookie;
begin
lHTTP := TIdHTTP.Create(nil);
try
IdSSL := TIdSSLIOHandlerSocketOpenSSL.Create(lHTTP);
IdSSL.SSLOptions.Method := sslvTLSv1;
IdSSL.SSLOptions.Mode := sslmClient;
lHTTP.IOHandler := IdSSL;
lHTTP.ReadTimeout := 30000;
lHTTP.HandleRedirects := True;
lHTTP.Request.UserAgent := 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.76 Mobile Safari/537.36';
lHTTP.Get('https://www.mywebserver.com', TStream(nil));
Cookie := lHTTP.CookieManager.CookieCollection.Cookie['id', 'www.mywebserver.com'];
if Cookie <> nil then
Access := Cookie.Value;
finally
end;

Params := TStringList.Create;
Reply := TStringList.Create;

TTask.Create(
procedure
var
N, m, i : integer;
begin
Params.Add('IDs=' +B.Strings[i]);
try
lHTTP.Request.CustomHeaders.Values['id'] := Access;
lHTTP.Request.CustomHeaders.Values['X-Requested-With'] := 'XMLHttpRequest';
lHTTP.Request.Referer := 'https://www.mywebserver.com/';
lHTTP.Request.ContentType := 'application/x-www-form-urlencoded';
lHTTP.Request.UserAgent := 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.76 Mobile Safari/537.36';
lHTTP.ConnectTimeout := 0;
Reply.Text := lHTTP.Post('https://www.mywebserver.com/', Params);

if AnsiContainsStr(Reply.Text, 'nothing') then
begin
TThread.Synchronize(nil,
procedure
begin
Memo1.Lines.Add(B.Strings[i]);
Label2.Caption := Memo1.Lines.Count.ToString;
end
);
end;
finally
Reply.Clear;
end;
end
).Start;
end;

Answer

In addition to what technicolor said, TTask in Delphi 10.1 Berlin has known timing issues, such as:

RSP-15233: TTask Stuck and take a very very long time to start

RSP-12557: TTask serialize parallel tasks without apparent reason

To avoid these issues, try using TThread.CreateAnonymousThread() instead of TTask.

Comments