有没有办法为Indy Tidhttp获取设置响应超时? [英] Is there a way to set response timeout for Indy Tidhttp gets?

查看:454
本文介绍了有没有办法为Indy Tidhttp获取设置响应超时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用Indy TIdhttp组件构建了一个简单的网站监控应用程序。我想检测指定的页面何时在指定的时间范围内没有返回(我正在使用5000毫秒)。作为测试,我在网站上创建了一个有意需要15秒的响应的页面。但是5秒钟后我无法让我的程序放弃。我尝试了 ReadTimeout 建议的解决方案使用一个计时器和 OnWorkBegin 事件(从来没有得到 OnWorkBegin 在get调用后立即触发)。



注意我不担心连接超时。我关心的是服务器返回页面的超时时间。



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

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

程序TServLogic.GetTimerTimer(Sender:TObject);
begin
idHttp.Disconnect(True);
结束

程序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);
除了
对e:exception do begin
AppendToLog('Server没有响应5秒');
结束
结束
finally
GetTimer.Free;
idhttp.free;
结束
结束


解决方案

我最终根据Rob的评论得到答案肯尼迪我多次尝试联络他,要求他提出一个正式的答复,以便我可以给他投票。从来没有听说过。


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;

解决方案

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.

这篇关于有没有办法为Indy Tidhttp获取设置响应超时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆