加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-15 04:05:37 所属栏目:大数据 来源:网络整理
导读:我使用Indy TIdhttp组件构建了一个简单的网站监控应用程序.我想检测何时在指定的时间范围内没有返回指定的页面(我使用5000毫秒).作为测试,我在网站上创建了一个页面,故意需要15秒才能响应.但我不能让我的程序在5秒后“放弃”.我已经尝试过ReadTimeout,一个使
我使用Indy TIdhttp组件构建了一个简单的网站监控应用程序.我想检测何时在指定的时间范围内没有返回指定的页面(我使用5000毫秒).作为测试,我在网站上创建了一个页面,故意需要15秒才能响应.但我不能让我的程序在5秒后“放弃”.我已经尝试过ReadTimeout,一个使用计时器的 suggested solution和OnWorkBegin事件(在get调用之后永远无法让OnWorkBegin立即触发).

注意我并不担心连接超时.我担心的是服务器返回页面的超时.

这是我一直在使用的一些源代码.它包含我参考的许多元素.

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;

解决方法

我最终根据Rob Kennedy的评论得到了答案.我曾多次尝试与他联系并要求他做出“正式”回答,以便我可以给他投票.从未收到过回复.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读