check-host.net API 的 Java 實現
如果您遇到任何問題,請在問題追蹤器上報告。
如果您只是想談談或需要有關 CheckHost4J 的協助,請隨時加入我的 Discord。
要將 CheckHost4J 與 Gradle/Maven 一起使用,您可以使用 Maven Central、Lenni0451 的 Maven 或 Jitpack。
您還可以在那裡找到如何將其實現到建置腳本中的說明。
如果您只需要最新的 jar 文件,可以從 GitHub Actions 下載它或使用 Release。
這個函式庫要求你的類路徑中有 Gson
API主類別是CheckHost4J
,它包含與API互動的所有方法。
final CheckHost4J checkHost = CheckHost4J . INSTANCE ;
您可以使用CheckHost4J.INSTANCE
或自行建立CheckHost4J
的新實例,透過建立自己的實例,您可以自行定義IRequester
,該實例用於將請求傳送至 API。
final CheckHost4J checkHost = new CheckHost4J (...);
預設的IRequester
是JavaRequester
,可以透過JavaRequester.INSTANCE
存取它,也可以使用new JavaRequester("<user-agent>")
建立一個新實例。
final CheckHost4J checkHost = new CheckHost4J ( new JavaRequester ( "MyUserAgent" ));
您可以使用方法CheckHost4J#ping
、 CheckHost4J#http
、 CheckHost4J#tcpPort
、 CheckHost4J#udpPort
和CheckHost4J#dns
來取得ResultNode<T>
,其中 T 是請求的結果類型(例如PingResult
、 TCPResult
)。
final ResultNode < PingResult > pingResult = checkHost . ping ( "example.com" , 80 /* max nodes */ );
取得ResultNode<T>
後,您可以使用tickResults()
更新getResults()
清單。
// This will update the results list by sending the check-result request to the API,
// This might not update all results, because some might not be finished yet
// Which means you have to call this method multiple times to get all results (e.g. with a delay of 5 seconds)
pingResult . tickResults ();
final Map < ServerNode , PingResult > results = pingResult . getResults ();
results . forEach (( serverNode , result ) -> {
if ( result == null ) { // All results which are not finished yet will be null
System . out . println ( serverNode . name + " is still checking..." );
} else if ( result . getErrorMessage () != null ) {
System . out . println ( serverNode . name + " failed: " + result . getErrorMessage ());
} else {
System . out . println ( serverNode . name + " responded: " + result . getSuccessfulPings () + "/" + result . getTotalPings ());
}
});
您也可以使用getNodes()
方法來取得正在檢查的所有伺服器節點。
final List < ServerNode > nodes = pingResult . getNodes ();
de.florianmichael.checkhost4j.model.result
套件包含所有結果類,用於儲存請求的結果。
若要取得所有請求類型的列表,您可以使用ResultType
枚舉。