2016-07-31 75 views
1

我寫了一個簡單的程序,可以定期檢查數據。問題在於檢查過程隨着時間的推移而減慢。首先它在一秒鐘內發送2個帖子,在45秒之後發送一個帖子,最終完全停止。我不知道這裏有什麼問題。如何快速分析HTTP Post響應

procedure TForm2.CheckURLs; 
var 
    lHTTP: TIdHTTP; 
    IdSSL : TIdSSLIOHandlerSocketOpenSSL; 
    N: Integer; 
    Access: string; 
    Params, Reply: TStringList; 
    Cookie: TIdCookie; 
begin 
    lHTTP := TIdHTTP.Create(nil); 
    try 
    IdSSL := TIdSSLIOHandlerSocketOpenSSL.Create(lHTTP); 
    IdSSL.SSLOptions.Method := sslvTLSv1; 
    IdSSL.SSLOptions.Mode := sslmClient; 
    lHTTP.IOHandler := IdSSL; 
    lHTTP.ReadTimeout := 30000; 
    lHTTP.HandleRedirects := True; 
    lHTTP.Request.UserAgent := 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.76 Mobile Safari/537.36'; 
    lHTTP.Get('https://www.mywebserver.com', TStream(nil)); 
    Cookie := lHTTP.CookieManager.CookieCollection.Cookie['id', 'www.mywebserver.com']; 
    if Cookie <> nil then 
     Access := Cookie.Value; 
    finally 
    end; 

    Params := TStringList.Create; 
    Reply := TStringList.Create; 

    TTask.Create(
    procedure 
    var 
     N, m, i : integer; 
    begin 
     Params.Add('IDs=' +B.Strings[i]); 
     try 
     lHTTP.Request.CustomHeaders.Values['id'] := Access; 
     lHTTP.Request.CustomHeaders.Values['X-Requested-With'] := 'XMLHttpRequest'; 
     lHTTP.Request.Referer := 'https://www.mywebserver.com/'; 
     lHTTP.Request.ContentType := 'application/x-www-form-urlencoded'; 
     lHTTP.Request.UserAgent := 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.76 Mobile Safari/537.36'; 
     lHTTP.ConnectTimeout := 0; 
     Reply.Text := lHTTP.Post('https://www.mywebserver.com/', Params); 

     if AnsiContainsStr(Reply.Text, 'nothing') then 
     begin 
      TThread.Synchronize(nil, 
      procedure 
      begin 
       Memo1.Lines.Add(B.Strings[i]); 
       Label2.Caption := Memo1.Lines.Count.ToString; 
      end 
     ); 
     end;   
     finally 
     Reply.Clear; 
     end; 
    end 
).Start; 
end; 

回答

2

除了所說的Technicolor,TTask德爾福10.1柏林已經知道時間的問題,如:

RSP-15233: TTask Stuck and take a very very long time to start

RSP-12557: TTask serialize parallel tasks without apparent reason

爲了避免這些問題, ,請嘗試使用TThread.CreateAnonymousThread()而不是TTask

+0

我測試了RSP-15233,果然只有大約9個任務開始。經過一些測試後,我相信會發生什麼是一個任務被髮送到主線程,並且睡眠呼叫會阻止其他人在至少一段時間內啓動。 添加SyncObject解決了我的問題: x:= 0; 對於i:= 1到25做 TTask.Create(程序開始 的OutputDebugString(PCHAR( '開始::' + TInterlocked.Increment(X)的ToString)); 睡眠(MAXINT); 端)。開始; – FredS

+0

使用了AnonymousThreads,沒有更多的緩慢。 –

0

你的緩慢可能來自你的PROGRAMM這兩個部分: 下載DATAS或對他們的工作。

根據經驗,我懷疑網站會讓你放慢速度,所以你可以嘗試在下載程序後評論一切(當然要小心)。再次運行這個程序應該給出相同的結果,然後在網站上給我們確認「錯誤」。

爲避免被防機器人保護引起的45秒阻塞,請嘗試通過在每次請求後添加幾秒暫停來預防性地減慢程序速度。 即使沒有保護措施,減慢我們的webbots也是一個很好的做法。我們不這樣做的DoS :)