我需要一个监视器类来定期检查给定的HTTP URL是否可用。我可以使用Spring TaskExecutor抽象来处理“常规”部分,因此这不是主题。问题是:在java中ping网址的首选方法是什么?
以下是我当前的代码作为起点:
try {
final URLConnection connection = new URL(url).openConnection();
connection.connect();
LOG.info("Service " + url + " available, yeah!");
available = true;
} catch (final MalformedURLException e) {
throw new IllegalStateException("Bad URL: " + url, e);
} catch (final IOException e) {
LOG.info("Service " + url + " unavailable, oh no!", e);
available = false;
}
GET
请求。有没有办法发送HEAD
?答案 0 :(得分:252)
这有什么用处(它能做我想做的吗?)
你可以这样做。另一种可行的方法是使用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.
}
}
boolean reachable = InetAddress.getByName(hostname).isReachable();
然而,这并未明确测试端口80.由于防火墙阻止其他端口,您可能会出现漏报。
我是否必须以某种方式关闭连接?
不,你没有明确需要。它被处理并汇集在引擎盖下。
我想这是一个GET请求。有没有办法发送HEAD?
您可以将获得的URLConnection
转换为HttpURLConnection
,然后使用setRequestMethod()
设置请求方法。但是,您需要考虑到一些糟糕的Web应用程序或自行开发的服务器可能会返回HTTP 405 error一个HEAD(即不可用,未实现,不允许),而GET工作正常。如果您打算验证链接/资源而非域/主机,则使用GET更可靠。
在我的情况下测试服务器的可用性是不够的,我需要测试URL(可能没有部署webapp)
实际上,连接主机只会通知主机是否可用,而不是内容可用。 Web服务器启动时没有问题,但是在服务器启动期间无法部署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 section 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;
}
}
答案 1 :(得分:17)
通过在URL对象上调用openConnection(),而不是使用URLConnection来使用HttpURLConnection。
然后,一旦您从连接中读取,使用getResponseCode()将为您提供HTTP响应。
这是代码:
HttpURLConnection connection = null;
try {
URL u = new URL("http://www.google.com/");
connection = (HttpURLConnection) u.openConnection();
connection.setRequestMethod("HEAD");
int code = connection.getResponseCode();
System.out.println("" + code);
// You can determine on HTTP return code received. 200 is success.
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();
}
}
同时检查类似问题How to check if a URL exists or returns 404 with Java?
希望这有帮助。
答案 2 :(得分:7)
您还可以使用HttpURLConnection,它允许您设置请求方法(例如HEAD)。 Here's an example,显示如何发送请求,阅读响应和断开连接。
答案 3 :(得分:4)
以下代码执行HEAD
请求以检查网站是否可用。
public static boolean isReachable(String targetUrl) throws IOException
{
HttpURLConnection httpUrlConnection = (HttpURLConnection) new URL(
targetUrl).openConnection();
httpUrlConnection.setRequestMethod("HEAD");
try
{
int responseCode = httpUrlConnection.getResponseCode();
return responseCode == HttpURLConnection.HTTP_OK;
} catch (UnknownHostException noInternetConnection)
{
return false;
}
}
答案 4 :(得分:3)
here作者建议:
public boolean isOnline() {
Runtime runtime = Runtime.getRuntime();
try {
Process ipProcess = runtime.exec("/system/bin/ping -c 1 8.8.8.8");
int exitValue = ipProcess.waitFor();
return (exitValue == 0);
} catch (IOException | InterruptedException e) { e.printStackTrace(); }
return false;
}
可能的问题
阅读链接。它看起来非常好
编辑: 在我使用它的过程中,它没有这种方法快:
public boolean isOnline() {
NetworkInfo netInfo = connectivityManager.getActiveNetworkInfo();
return netInfo != null && netInfo.isConnectedOrConnecting();
}
它们有点不同但是在检查互联网连接的功能中,由于连接变量,第一种方法可能会变慢。
答案 5 :(得分:2)
考虑使用Restlet框架,它对这类事物具有很好的语义。它功能强大且灵活。
代码可以简单如下:
Client client = new Client(Protocol.HTTP);
Response response = client.get(url);
if (response.getStatus().isError()) {
// uh oh!
}
答案 6 :(得分:0)
wrt 2:你最好关闭它。但是,它可能取决于所使用的URLConnection的特定实现。我刚刚完成了追踪系统资源泄漏的原因。一个应用程序生成了许多挂起的连接(根据lsof;我们在JDK1.6上运行它),原因是我们已经使用了你所展示的代码。 TCP连接没有关闭,例如返回游泳池等 - 他们处于ESTABILISHED状态。在这种情况下,正确的场景是由YoK显示的场景 - 将其转换为(HttpURLConnection)并调用.disconnect()。