Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

delphi

indy

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;
like image 810
M Schenkel Avatar asked Jul 23 '10 02:07

M Schenkel


2 Answers

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
like image 119
dan-gph Avatar answered Oct 04 '22 03:10

dan-gph


I eventually got the answer based on the comments of Rob Kennedy. I tried numerous times to contact him and request he make a "formal" answer so that I could give him the vote. Never heard back.

like image 32
M Schenkel Avatar answered Oct 04 '22 03:10

M Schenkel