Java Ping ping(String url, int timeout)

Here you can find the source of ping(String url, int timeout)

Description

Found on http://stackoverflow.com/questions/3584210/preferred-java-way-to-ping-a-http-url-for-availability Pings a HTTP URL.

License

Open Source License

Parameter

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.

Return

true if the given HTTP URL has returned response code 200-399 on a HEAD request within the given timeout, otherwise false.

Declaration

public static boolean ping(String url, int timeout) 

Method Source Code


//package com.java2s;
/*/*from  ww  w .  j a  v a 2  s . c o  m*/
 * Copyright (c) 2014 Magnet Systems, Inc.
 * All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you
 * may not use this file except in compliance with the License. You
 * may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */

import sun.net.www.protocol.file.FileURLConnection;

import java.io.IOException;

import java.net.HttpURLConnection;

import java.net.URL;
import java.net.URLConnection;

public class Main {
    /**
     * Found on http://stackoverflow.com/questions/3584210/preferred-java-way-to-ping-a-http-url-for-availability
     * 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 ping(String url, int timeout) {
        try {
            URLConnection conn = new URL(url).openConnection();
            if (conn instanceof FileURLConnection) {
                return true;
            }
            HttpURLConnection connection = (HttpURLConnection) conn;
            connection.setConnectTimeout(timeout);
            connection.setReadTimeout(timeout);
            connection.setRequestMethod("HEAD");
            int responseCode = connection.getResponseCode();
            return (200 <= responseCode && responseCode <= 399);
        } catch (IOException exception) {
            return false;
        }
    }
}

Related

  1. ping(String ip)
  2. pingHost(String host)
  3. ping(String url, int timeout)
  4. pingHttp(URL url, int retry, int timeout)
  5. pingUrl(final String address)
  6. pingURL(final String url)