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.
Hi
I want to ping the endpoint for a post url. Is that feasible?
Hi Soumik – Yes. Absolutely. You could do that. You may need to create Post service.
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.
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.
For all the websites it is showing as “red” but no error/exception. Someone please help
Hi Vinitha – are you running this a root? Java Ping need root access.
Hi Vinitha – can you give it a try again? I’ve added more debugging to code.
Same here. I did not get any error but the status is showing as “red” for all websites. Someone please help.
Hi Vinitha – please check my above comment about root privilege.
Giving red for every website
What error are you getting Harsh?
Not getting any error but getting red status for all websites
Working now but getting connection time out error for one URL
What was an issue Harsh? I’m glad it’s working now.
Hello Harsh,
Please what did you do to resolve your “red status issue” please ?
Thank you in advance for your help.
Hi Oumar – what issue are you facing?
I’ve just updated above program and added more debugging. Let me know how it goes. Waiting for your update.
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..
Great scenario. I’ll look at your requirement in details and update.
Hi there – It’s complaining about main class 🙂 This happens when you have Eclipse environment and Java issue.
Hello, please can explain to me please the role of this program ??
Exactly the same purpose as “Pingdom: https://www.pingdom.com/” 🙂