How to get Ping Status of any HTTP End Point in Java?

Last updated
App Shah

Crunchify » Java and J2EE Tutorials » How to get Ping Status of any HTTP End Point in Java?

Get HTTP Ping Status in Java

I’m sure while working on Java Web Project, you must have faced these kind of questions:

  • How to get HTTP response code for a URL in Java?
  • How to check if a URL exists or returns 404 with Java?
  • How can I read the status code of a HTTP request?
  • How to get HTTP code from org.apache.http.HttpResponse?
  • How to check if my Tomcat is up and running?

Well, below simple Java Program will answer all your mentioned questions. Do let me know if you see any problem.

package crunchify.com.tutorials;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * @author crunchify.com
 * 
 */

public class CrunchifyGetPingStatus {
	public static void main(String args[]) throws Exception {

		String[] hostList = { "https://crunchify.com", "https://yahoo.com", "https://www.ebay.com", 
				"https://google.com",
				"https://www.example.co", "https://paypal.com", 
				"https://bing.com/", "https://techcrunch.com/", "https://mashable.com/",
				"https://thenextweb.com/", "https://wordpress.com/", 
				"https://wordpress.org/", "https://example.com/", "https://sjsu.edu/",
				"https://ebay.co.uk/", "https://google.co.uk/", "https://wikipedia.org/" };

		for (int i = 0; i < hostList.length; i++) {

			String url = hostList[i];
			getStatus(url);

		}

		System.out.println("Task completed...");
	}

	public static String getStatus(String url) throws IOException {

		String result = "";
		int code = 200;
		try {
			URL siteURL = new URL(url);
			HttpURLConnection connection = (HttpURLConnection) siteURL.openConnection();
			connection.setRequestMethod("GET");
			connection.setConnectTimeout(3000);
			connection.connect();

			code = connection.getResponseCode();
			if (code == 200) {
				result = "-> Green <-\t" + "Code: " + code;
				;
			} else {
				result = "-> Yellow <-\t" + "Code: " + code;
			}
		} catch (Exception e) {
			result = "-> Red <-\t" + "Wrong domain - Exception: " + e.getMessage();

		}
		System.out.println(url + "\t\tStatus:" + result);
		return result;
	}

}

Output:

https://crunchify.com		Status:-> Yellow <-	Code: 301
https://yahoo.com		Status:-> Yellow <-	Code: 301
https://www.ebay.com		Status:-> Yellow <-	Code: 301
https://google.com		Status:-> Green <-	Code: 200
https://www.example.co		Status:-> Red <-	Wrong domain - Exception: www.example.co
https://paypal.com		Status:-> Green <-	Code: 200
https://bing.com/		Status:-> Green <-	Code: 200
https://techcrunch.com/		Status:-> Yellow <-	Code: 301
https://mashable.com/		Status:-> Yellow <-	Code: 301
https://thenextweb.com/		Status:-> Green <-	Code: 200
https://wordpress.com/		Status:-> Yellow <-	Code: 301
https://wordpress.org/		Status:-> Yellow <-	Code: 301
https://example.com/		Status:-> Green <-	Code: 200
https://sjsu.edu/		Status:-> Red <-	Wrong domain - Exception: connect timed out
https://ebay.co.uk/		Status:-> Yellow <-	Code: 301
https://google.co.uk/		Status:-> Green <-	Code: 200
https://wikipedia.org/		Status:-> Yellow <-	Code: 301
Task completed...

If you are interested on more than 10 java tutorials then check this out.

22 thoughts on “How to get Ping Status of any HTTP End Point in Java?”

  1. Hi
    Good to see this solution. But we have some SSL Certificates to connect client. How can we use this solution by validating our certificates.

    Reply
    • Hi manoj kumar – try capturing SSLCert error and pass through it. Just simply consume it and proceed with your code logic.

      I’ll try to create tutorial on SSL Cert Validation in Java in few days. Please stay tuned.

      Reply
  2. Same here. I did not get any error but the status is showing as “red” for all websites. Someone please help.

    Reply
  3. Hi,
    Really helpful article.. But how can we change this code to be operated in a Proxy based network?? How to insert information related to IP, Port, Username , Password Etc. Do explain it please…

    Thanks..

    Reply
  4. Exception in thread "main" java.lang.NoClassDefFoundError: CrunchifyGetPingStatu
    s (wrong name: com/crunchify/tutorials/CrunchifyGetPingStatus)
            at java.lang.ClassLoader.defineClass1(Native Method)
            at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
            at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
            at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:14
    1)
            at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
            at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
            at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
            at java.security.AccessController.doPrivileged(Native Method)
            at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
            at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    Could not find the main class: CrunchifyGetPingStatus.  Program will exit.
    Reply
    • Hi there – It’s complaining about main class 🙂 This happens when you have Eclipse environment and Java issue.

      Reply

Leave a Comment