通过连接验证网址
import java.io.BufferedReader;/** n o w j a v a . c o m 提 供 **/ import java.io.IOException; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection; import org.apache.log4j.Logger; public class Main{ public static void main(String[] argv) throws Exception{ String checkURL = "nowjava.com"; System.out.println(validateURL(checkURL)); } private static final String SORRY_PAGE = "Sorry, the page you requested was not found"; /* *来 自 n o w j a v a . c o m - 时代Java */ private static Logger log = Logger.getLogger(CommonUtils.class .getName()); /** * @param checkURL * @return */ public static boolean validateURL(String checkURL) { try { URL url = new URL(checkURL); URLConnection connection = url.openConnection(); connection.connect(); if (connection instanceof HttpURLConnection) { HttpURLConnection httpConnection = (HttpURLConnection) connection; httpConnection.setConnectTimeout(500); int code = httpConnection.getResponseCode(); String pageContents = getPageContents(connection); if (((code == 200 || code == 301 || code == 302 || code == 403))) { if (!pageContents.contains(SORRY_PAGE)) { //&& (!pageContents.contains(SEARCH_PAGE))) { return true; } else { log.error("Redirected to yahoo search page " + checkURL); return false; } } else { log.error(checkURL + " returned - " + code); return false; } } else { log.error("error - not a http request!"); return false; } } catch (Exception e) { log.error("Validation failed " + e.getMessage()); return false; } } /** * @param connection * @return * @throws IOException */ public static