这有什么好处吗(会做我想要的吗?)
你可以这样做。另一种可行的方法是使用
java.net.Socket。
public static boolean pingHost(String host, int port, int timeout) { try (Socket socket = new Socket()) { socket.connect(new InetSocketAddress(host, port), timeout); return true; } catch (IOException e) { return false; // Either timeout or unreachable or failed DNS lookup. }}还有InetAddress#isReachable():
boolean reachable = InetAddress.getByName(hostname).isReachable();
但是,这并没有明确测试端口80。由于防火墙阻止了其他端口,因此你可能会得到误报。
我是否必须以某种方式关闭连接?
不,你不需要。它被处理并收集在引擎盖下。
我想这是一个GET请求。有没有办法发送HEAD?
你可以将获得的内容转换URLConnection为HttpURLConnection,然后用于setRequestMethod()设置请求方法。但是,你需要考虑到,当GET正常运行时,某些性能较差的Web应用程序或本地服务器可能为HEAD 返回HTTP 405错误(即,不可用,未实现,不允许)。如果你打算验证链接/资源而不是域/主机,则使用GET更可靠。
在我的情况下,仅测试服务器的可用性是不够的,我需要测试URL(可能未部署webapp)
实际上,连接主机仅通知主机是否可用,而不通知内容是否可用。可能发生的情况是,网络服务器启动时没有问题,但是在服务器启动期间Webapp部署失败。但是,这通常不会导致整个服务器宕机。你可以通过检查HTTP响应代码是否为200来确定。
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();connection.setRequestMethod("HEAD");int responseCode = connection.getResponseCode();if (responseCode != 200) { // Not OK.}// < 100 is undetermined.// 1nn is informal (shouldn't happen on a GET/HEAD)// 2nn is success// 3nn is redirect// 4nn is client error// 5nn is server error有关响应状态代码的更多详细信息,请参阅RFC 2616第10节。connect()如果要确定响应数据,则无需进行呼叫。它将隐式连接。
为了方便将来参考,下面是一个实用工具方法的完整示例,其中还考虑了超时:
public static boolean pingURL(String url, int timeout) { url = url.replaceFirst("^https", "http"); // Otherwise an exception may be thrown on invalid SSL certificates. try { HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); connection.setConnectTimeout(timeout); connection.setReadTimeout(timeout); connection.setRequestMethod("HEAD"); int responseCode = connection.getResponseCode(); return (200 <= responseCode && responseCode <= 399); } catch (IOException exception) { return false; }}


