简体   繁体   English

有没有办法为Indy Tidhttp设置响应超时?

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

I have built a simple website monitoring application using Indy TIdhttp component. 我使用Indy TIdhttp组件构建了一个简单的网站监视应用程序。 I want to detect when a designated page is not returned within a specified time frame (I am using 5000 milliseconds). 我想检测在指定的时间范围内未返回指定的页面(我使用的是5000毫秒)。 As a test I created a page on a web site which intentionally takes 15 seconds to respond. 作为测试,我在网站上创建了一个页面,该页面有意识地需要15秒才能响应。 But I can't get my procedure to "give up" after the 5 seconds. 但是我无法让我的程序在5秒后“放弃”。 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). 我尝试了ReadTimeout ,这是一个建议的解决方案,它使用计时器和OnWorkBegin事件(在get调用之后,永远无法使OnWorkBegin立即启动)。

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;

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: 这不能回答上面的特定问题,但是由于这是Google搜索“ indy http timeout”时会带您到的地方,因此我在这里将提及您可以设置以下属性:

TIdHTTP.ReadTimeout
TIdHTTP.ConnectTimeout

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. 再也没有听到回音。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM