Is there a way to set response timeout for Indy Tidhttp gets?

M Schenkel picture M Schenkel · Jul 23, 2010 · Viewed 21.6k times · Source

I have built a simple website monitoring application using Indy TIdhttp component. I want to detect when a designated page is not returned within a specified time frame (I am using 5000 milliseconds). As a test I created a page on a web site which intentionally takes 15 seconds to respond. But I can't get my procedure to "give up" after the 5 seconds. I have tried ReadTimeout, a suggested solution using a timer and the OnWorkBegin event (was never able to get OnWorkBegin to fire immediately after the get call).

Note I am not worried about a connection timeout. My concern here is a timeout for the server to return with a page.

Here is some source code I have been using. It contains many of the elements I reference.

procedure TServLogic.WorkBegin(ASender: TObject; AWorkMode: TWorkMode; AWorkCountMax: Int64);
begin
  GetTimer.Enabled := True;
end;
procedure TServLogic.WorkEnd(ASender: TObject; AWorkMode: TWorkMode);
begin
  GetTimer.Enabled := False;
end;

procedure TServLogic.GetTimerTimer(Sender: TObject);
begin
  idHttp.Disconnect(True);
end;

procedure TServLogic.CallHttp(mlink: String): String;
begin
  result := '';
  GetTimer := TTimer.create(nil);
  GetTimer.OnTimer := GetTimerTimer;
  GetTimer.Interval := 5000;
  try
    IdHTTP := TIdHTTP.create(nil);
    idhttp.ReadTimeout := 5000;
    IdHttp.OnWorkBegin := WorkBegin;
    IdHttp.OnWorkEnd   := WorkEnd;
    try
      result  := idhttp.get(mLink);
    except
      on e:exception do begin
        AppendToLog('Server did not respond withing 5 seconds');
      end;
    end;
  finally
    GetTimer.Free;
    idhttp.free;
  end;
end;

Answer

dan-gph picture dan-gph · Jul 4, 2018

This doesn't answer the particular question above, but since this is where Google takes you if you search for "indy http timeout", I will mention here that you can set these properties:

TIdHTTP.ReadTimeout
TIdHTTP.ConnectTimeout