2017-08-07 67 views
1

在特殊的診斷模式下,我通過在已知的總是預期的活着的服務器上執行HTTP GET操作來測試互聯網連接。雖然這通常非常快,但在遠程位置工作或禁用計算機網絡適配器時,這可能會在15秒或更長時間後變慢和/或超時。中斷HTTP GET操作

是否可以配置Indy組件,以便我可以根據需要中斷它?或者也許有更好的方法來執行測試HTTP GET操作?我的代碼如下,我可能會調用與HasInternet(http://master11.teamviewer.com/);

function HasInternet(strTestWebServer: String) : Boolean; 
var 
    idHTTP: TIdHTTP; 
begin 
    // Test an internet connection to the named server 
    Result := False; 
    idHTTP := TIdHTTP.Create(nil); 
    if (idHTTP <> nil) then 
     begin 
     try 
      try 
       // Handle redirects in response from HTTP server. Not required for some servers, 
       // but can confirm a connection to servers that redirect to https. 
       idHTTP.HandleRedirects := True; 

       // Add in the HTTP protocol header (if required) and retrieve the HTTP resource 
       // Note: HTTP_PROTOCOL = 'http://'; 
       if (AnsiPos(HTTP_PROTOCOL, strTestWebServer) > 0) then 
        Result := (idHTTP.Get(strTestWebServer) <> '') 
       else 
        Result := (idHTTP.Get(HTTP_PROTOCOL + strTestWebServer) <> ''); 
      except 
       // HTTP protocol errors are sometimes returned by servers, with the status code 
       // saved in TIdHTTP.ResponseCode. Common responses include: 
       // * 403 Forbidden (request was valid, but server is refusing to respond to it) 
       // * 404 Not Found (requested resource could not be found) 
       // * 405 Method Not Allowed (requested method not supported by that resource) 
       // These errors are counted as "valid connection to the internet possible" 
       on E: EIdHTTPProtocolException do 
        // Status code can be found in "E.ReplyErrorCode" or "idHTTP.ResponseCode" 
        Result := True; 
      else 
       // All other exceptions are counted as definite failures 
       Result := False; 
      end; 
     finally 
      idHTTP.Free(); 
     end; 
     end; 
end; 

回答

2

您可以通過從在執行該方法的一個不同的線程上下文調用Disconnect中斷運行TIdHTTP HTTP方法。使用你的設計,它意味着以某種方式暴露內部使用的TIdHTTP對象。

但是,爲了您的任務,你可能會給InternetCheckConnection功能一試。例如對於德爾福7它可能是:

const 
    FLAG_ICC_FORCE_CONNECTION = $00000001; 

function InternetCheckConnectionA(lpszUrl: PAnsiChar; dwFlags: DWORD; dwReserved: DWORD): BOOL; stdcall; 
    external 'wininet.dll' name 'InternetCheckConnectionA'; 

function InternetCanConnect(const URL: AnsiString): Boolean; 
begin 
    Result := Boolean(InternetCheckConnectionA(PAnsiChar(URL), FLAG_ICC_FORCE_CONNECTION, 0)); 
end; 

function InternetNotConnected(const URL: AnsiString): Boolean; 
begin 
    Result := not Boolean(InternetCheckConnectionA(PAnsiChar(URL), 
    FLAG_ICC_FORCE_CONNECTION, 0)) and (GetLastError = ERROR_NOT_CONNECTED); 
end; 
+0

謝謝!從來不知道「InternetCheckConnectionA」Windows功能。剛剛測試過它。在控制面板中禁用了網絡適配器的情況下,兩種方法在15秒或更長時間後仍然超時。但是在適配器正常工作的情況下,這種方法要快得多。 – AlainD

+0

請注意,跨線程斷開套接字並不總是適用於所有平臺。已知「InternetCheckConnection()」會報告不準確的結果。至少,你可以設置TIdHTTP的'ConnectTimeout'和'ReadTimeout'屬性,和/或在其OnWork ...事件中引發異常。 –

+0

此外,當您只對檢查Internet連接感興趣時,執行完整的HTTP請求/響應對有點矯枉過正。你可以直接使用'TIdTCPClient'並連接到一個衆所周知的服務器端口,而不用實際傳輸任何數據。 –