これはまったく良いですか(それは私が望むことをしますか?)
あなたはそうすることができます。別の実行可能な方法は、を使用すること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 が完全に正常に機能しているのに、貧弱な webapps または自家製サーバーが HEAD に対してHTTP 405 エラー(つまり、利用できない、実装されていない、許可されていない) を返す可能性があることを考慮する必要があります。ドメイン/ホストではなくリンク/リソースを検証する場合は、GET を使用する方が信頼性が高くなります。
私の場合、サーバーの可用性をテストするだけでは不十分です。URL をテストする必要があります (Web アプリケーションがデプロイされていない可能性があります)。
実際、ホストを接続しても、コンテンツが利用可能かどうかではなく、ホストが利用可能かどうかのみが通知されます。Web サーバーが問題なく起動したにもかかわらず、サーバーの起動中に Web アプリケーションの展開に失敗したということもあり得ます。ただし、通常、これによってサーバー全体がダウンすることはありません。これは、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()
ちなみに、応答データを決定している場合、呼び出しは必要ありません。暗黙的に接続されます。
今後の参考のために、タイムアウトも考慮した、ユーティリティ メソッドのフレーバーの完全な例を次に示します。
/**
* Pings a HTTP URL. This effectively sends a HEAD request and returns <code>true</code> if the response code is in
* the 200-399 range.
* @param url The HTTP URL to be pinged.
* @param timeout The timeout in millis for both the connection timeout and the response read timeout. Note that
* the total timeout is effectively two times the given timeout.
* @return <code>true</code> if the given HTTP URL has returned response code 200-399 on a HEAD request within the
* given timeout, otherwise <code>false</code>.
*/
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;
}
}