public static boolean checkUrl(String urlAsString){
  if (urlAsString == null) {
    logger.error("can not check null URL");
    return false;
  }
  URL url;
  try {
    url=new URL(urlAsString);
  }
 catch (  MalformedURLException e) {
    logger.error(urlAsString + " is not a valid url, can not check.");
    return false;
  }
  int responseCode;
  String responseMessage="NO RESPONSE MESSAGE";
  Object content="NO CONTENT";
  HttpURLConnection huc;
  try {
    huc=(HttpURLConnection)url.openConnection();
    huc.setRequestMethod("HEAD");
    responseCode=huc.getResponseCode();
    content=huc.getContent();
    responseMessage=huc.getResponseMessage();
  }
 catch (  ProtocolException e) {
    logger.error("can not check url " + e.getMessage(),e);
    return false;
  }
catch (  IOException e) {
    logger.error("can not check url " + e.getMessage(),e);
    return false;
  }
  if (responseCode == 200 || (responseCode > 300 && responseCode < 400)) {
    logger.info("URL " + urlAsString + " exists");
    return true;
  }
 else {
    logger.error(urlAsString + " return a " + responseCode+ " : "+ content+ "/"+ responseMessage);
    return false;
  }
}

public static boolean checkUrl ( String urlAsString ) { if ( urlAsString == null ) { logger . error ( "can not check null URL" ) ; return false ; } URL url ; try { url = new URL ( urlAsString ) ; } catch ( MalformedURLException e ) { logger . error ( urlAsString + " is not a valid url, can not check." ) ; return false ; } int responseCode ; String responseMessage = "NO RESPONSE MESSAGE" ; Object content = "NO CONTENT" ; HttpURLConnection huc ; try { huc = ( HttpURLConnection ) url . openConnection ( ) ; huc . setRequestMethod ( "HEAD" ) ; responseCode = huc . getResponseCode ( ) ; content = huc . getContent ( ) ; responseMessage = huc . getResponseMessage ( ) ; } catch ( ProtocolException e ) { logger . error ( "can not check url " + e . getMessage ( ) , e ) ; return false ; } catch ( IOException e ) { logger . error ( "can not check url " + e . getMessage ( ) , e ) ; return false ; } if ( responseCode == 200 || ( responseCode > 300 && responseCode < 400 ) ) { logger . info ( "URL " + urlAsString + " exists" ) ; return true ; } else { logger . error ( urlAsString + " return a " + responseCode + " : " + content + "/" + responseMessage ) ; return false ; } }
check if an url doesn ' t return 200 or 3xx code
