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枚举。