import javax.imageio.*; import java.awt.image.*; import java.awt.event.*; import java.awt.*; import java.security.NoSuchAlgorithmException; import java.security.MessageDigest; import java.lang.reflect.*; import java.net.*; import java.io.*; import javax.swing.text.*; import javax.swing.event.*; import javax.swing.*; import java.util.concurrent.atomic.*; import java.util.concurrent.*; import java.util.regex.*; import java.util.List; import java.util.zip.*; import java.util.*; public class main { static int score, max = 3; static boolean online; public static void main(String[] args) throws Exception { print("Online check!"); score = 0; check("tinybrain.de"); check("google.com"); check("bing.com"); online = score > 0; print("Score: " + score + "/3"); } static void check(String server) { boolean online = checkIfHttpServerIsOnline("tinybrain.de") == null; if (online) ++score; print(server + ": " + (online ? "ONLINE!" : "OFFLINE?")); } // null = online, Throwable = offline public static Throwable checkIfHttpServerIsOnline(String serverName) { try { String url = serverName.startsWith("http") ? serverName : "http://" + serverName + "/"; //HttpURLConnection.setFollowRedirects(false); // note : you may also need // HttpURLConnection.setInstanceFollowRedirects(false) HttpURLConnection con = (HttpURLConnection) new URL(url).openConnection(); con.setRequestMethod("HEAD"); int responseCode = con.getResponseCode(); //print("Response code for " + url + ": " + responseCode); // If we came this far, assume server is online. return null; //return (con.getResponseCode() == HttpURLConnection.HTTP_OK); } catch (Exception e) { return e; } } static void print() { System.out.println(); } static void print(Object o) { System.out.println(o); } static void print(long i) { System.out.println(i); } }