Check for the presence or absence of a URL

I need to check if the url exists. I want to write a servlet for this, i.e. Check if url exists. If the URL entered does not exist, it should return some message.

+7
java url
Nov 14 '10 at 14:12
source share
4 answers

Best HTTP Solution:

public static boolean exists(String URLName){ try { HttpURLConnection.setFollowRedirects(false); // note : you may also need // HttpURLConnection.setInstanceFollowRedirects(false) HttpURLConnection con = (HttpURLConnection) new URL(URLName).openConnection(); con.setRequestMethod("HEAD"); return (con.getResponseCode() == HttpURLConnection.HTTP_OK); } catch (Exception e) { e.printStackTrace(); return false; } } 

If you are looking for some other url try this code

  public static boolean exists(String URLName){ boolean result = false; try { url = new URL("ftp://ftp1.freebsd.org/pub/FreeBSD/"); //url = new URL("ftp://ftp1.freebsd.org/pub/FreeBSD123/");//this will fail input = url.openStream(); System.out.println("SUCCESS"); result = true; } catch (Exception ex) { Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex); } return result; } 

Source : http://www.rgagnon.com/javadetails/java-0059.html

+22
Nov 14 2018-10-14
source share

You can try the HTTP HEAD method to see if the server returns a 200 status code to a specific URL and acts accordingly.

See http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol and scroll down to Request Methods .

+1
Nov 14 2018-10-14
source share

You can create a connection, return an input stream, and check its value.

0
Nov 14 2018-10-14
source share

I used this bash script to check the urls, but I need to put all the files in the urls.csv file

 #!/bin/bash ############################################### # mailto: ggerman@gmail.com # checkurls # https://github.com/ggerman/checkurls/ # require curl ############################################### url() { cat urls.csv | replace | show } replace() { tr ',' ' ' } show() { awk '{print $1}' } url | \ while read CMD; do echo $CMD curl -Is $CMD | head -n 1 done 
0
Jul 15 '15 at 11:58
source share



All Articles