This is the full static method, which you can adapt to set waiting time and error code when IOException happens:
public static int getResponseCode(String address) {
return getResponseCode(address, 404);
}
public static int getResponseCode(String address, int defaultValue) {
try {
//Logger.getLogger(WebOperations.class.getName()).info("Fetching response code at " + address);
URL url = new URL(address);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(1000 * 5); //wait 5 seconds the most
connection.setReadTimeout(1000 * 5);
connection.setRequestProperty("User-Agent", "Your Robot Name");
int responseCode = connection.getResponseCode();
connection.disconnect();
return responseCode;
} catch (IOException ex) {
Logger.getLogger(WebOperations.class.getName()).log(Level.INFO, "Exception at {0} {1}", new Object[]{address, ex.toString()});
return defaultValue;
}
}