Java tutorial
//package com.java2s; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection; public class Main { /** * Return '' or error message if error occurs during URL connection. * * @param url The URL to ckeck * @return */ public static String getUrlStatus(String url) { URL u; URLConnection conn; int connectionTimeout = 500; try { u = new URL(url); conn = u.openConnection(); conn.setConnectTimeout(connectionTimeout); // TODO : set proxy if (conn instanceof HttpURLConnection) { HttpURLConnection httpConnection = (HttpURLConnection) conn; httpConnection.setInstanceFollowRedirects(true); httpConnection.connect(); httpConnection.disconnect(); // FIXME : some URL return HTTP200 with an empty reply from server // which trigger SocketException unexpected end of file from server int code = httpConnection.getResponseCode(); if (code == HttpURLConnection.HTTP_OK) { return ""; } else { return "Status: " + code; } } // TODO : Other type of URLConnection } catch (Exception e) { e.printStackTrace(); return e.toString(); } return ""; } }