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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
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 = { "http://crunchify.com", "http://yahoo.com", "http://www.ebay.com", "https://google.com", "http://www.example.co", "https://paypal.com", "http://bing.com/", "http://techcrunch.com/", "http://mashable.com/", "https://thenextweb.com/", "http://wordpress.com/", "http://wordpress.org/", "http://example.com/", "http://sjsu.edu/", "https://ebay.co.uk/", "http://google.co.uk/", "http://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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
http://crunchify.com Status:-> Yellow <- Code: 301 http://yahoo.com Status:-> Yellow <- Code: 301 http://www.ebay.com Status:-> Yellow <- Code: 301 https://google.com Status:-> Green <- Code: 200 http://www.example.co Status:-> Red <- Wrong domain - Exception: www.example.co https://paypal.com Status:-> Green <- Code: 200 http://bing.com/ Status:-> Green <- Code: 200 http://techcrunch.com/ Status:-> Yellow <- Code: 301 http://mashable.com/ Status:-> Yellow <- Code: 301 https://thenextweb.com/ Status:-> Green <- Code: 200 http://wordpress.com/ Status:-> Yellow <- Code: 301 http://wordpress.org/ Status:-> Yellow <- Code: 301 http://example.com/ Status:-> Green <- Code: 200 http://sjsu.edu/ Status:-> Red <- Wrong domain - Exception: connect timed out https://ebay.co.uk/ Status:-> Yellow <- Code: 301 http://google.co.uk/ Status:-> Green <- Code: 200 http://wikipedia.org/ Status:-> Yellow <- Code: 301 Task completed... |
If you are interested on more than 10 java tutorials then check this out.