Here you can find the source of ping(String url, int timeout)
Parameter | Description |
---|---|
url | The HTTP URL to be pinged. |
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. |
true
if the given HTTP URL has returned response code 200-399 on a HEAD request within the given timeout, otherwise false
.
public static boolean ping(String url, int timeout)
//package com.java2s; /**/* www . j a v a 2s . com*/ * BioJava development code * * This code may be freely distributed and modified under the terms of the GNU * Lesser General Public Licence. This should be distributed with the code. If * you do not have a copy, see: * * http://www.gnu.org/copyleft/lesser.html * * Copyright for this code is held jointly by the individual authors. These * should be listed in @author doc comments. * * For more information on the BioJava project and its aims, or to join the * biojava-l mailing list, visit the home page at: * * http://www.biojava.org/ * * Created on Feb 23, 2012 Created by Andreas Prlic * * @since 3.0.2 */ import java.io.IOException; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection; public class Main { /** * 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>. * @author BalusC, * http://stackoverflow.com/questions/3584210/preferred-java-way-to-ping-a-http-url-for-availability */ public static boolean ping(String url, int timeout) { //url = url.replaceFirst("https", "http"); // Otherwise an exception may be thrown on invalid SSL certificates. try { HttpURLConnection connection = (HttpURLConnection) prepareURLConnection(url, timeout); connection.setRequestMethod("HEAD"); int responseCode = connection.getResponseCode(); return (200 <= responseCode && responseCode <= 399); } catch (IOException exception) { return false; } } /** * Prepare {@link URLConnection} with customised timeouts. * * @param url The URL * @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. * * <p> * Example of code. <code> * UrlConnection conn = prepareURLConnection("http://www.google.com/", 20000); * conn.connect(); * conn.getInputStream(); * </code> * <p> * * <bold>NB. User should execute connect() method before getting input * stream.</bold> * @return * @throws IOException * @author Jacek Grzebyta */ public static URLConnection prepareURLConnection(String url, int timeout) throws IOException { URLConnection connection = new URL(url).openConnection(); connection.setReadTimeout(timeout); connection.setConnectTimeout(timeout); return connection; } }