2010-07-23 131 views
7

我已經使用Indy TIdhttp組件構建了一個簡單的網站監視應用程序。我想檢測指定頁面在指定時間範圍內沒有返回(我正在使用5000毫秒)。作爲一項測試,我在一個網站上創建了一個頁面,故意花費15秒的時間做出迴應。但是我無法讓我的程序在5秒後「放棄」。我一直在使用一個定時器和OnWorkBegin事件試圖ReadTimeout,一個suggested solution(從來沒有能夠得到OnWorkBegin到get調用後立即開火)。有沒有辦法爲Indy Tidhttp設置響應超時?

注意我不擔心連接超時。這裏我關心的是服務器返回頁面的超時。

這是我一直在使用的一些源代碼。它包含了許多我參考的元素。

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; 
+0

另請參見:[使用Indy時連接不超時](http://stackoverflow.com/questions/3187446/the-connection-does-not-timeout-while-using-indy)。 – 2010-07-23 04:41:45

+2

你的邏輯與TTimer預計異步TIdHTTP這是不正確 – 2010-07-23 07:54:15

+0

看了羅布肯尼迪建議的文章。我創建了一個線程類型的解決方案。在主應用程序中創建一個TStringList來管理我的線程。我也有一個計時器,每5秒觸發一次。所以如果我看到一個線程在5秒鐘內沒有「完成」,我就調用線程的Terminate方法。但是這似乎並不像我想要的那樣立即「終止」它;它似乎仍然在等待。 – 2010-07-23 16:10:56

回答

2

我最終根據Rob Kennedy的評論得出了答案。我多次嘗試與他聯繫並要求他作出「正式」的回答,以便我可以給他投票。從未聽過。

相關問題