I need to check whether a URL exists or not. I want to write a servlet for this i.e. to check whether a URL exists or not. If the URL entered does not exist, then it should return some message.
Asked
Active
Viewed 2.7k times
9
-
6A URL cannot in general be said to not exist. – SLaks Nov 14 '10 at 14:14
4 Answers
26
Better solution for HTTP :
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 any 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

jmj
- 237,923
- 42
- 401
- 438
-
2
-
-
I need to use Httpclient and its methods to check for url existence can u please tell how to use httpClient service.I tried to write this service in one servelt but it is giving exception. – ha22109 Nov 14 '10 at 16:46
-
@ha22109 if you are only concern with HTTP then first code should work for you perfectly – jmj Nov 14 '10 at 16:53
-
1
-
@Michael Konietzka by URL I mean address to some resource above can't be checked for validity ,in such cases you need to contact @example.com – jmj Nov 14 '10 at 17:14
-
1
-
@Michael Konietzka OP is just concern about HTTP protocol :) it is not given in requirement list ;) – jmj Nov 14 '10 at 17:18
-
1I cannot see this in the requirements. ;-) OP is talking from URLs in general. ;-). Additionally the interpretation of `exist` is not clear. What does existence of a URL mean? From my point of view, the URL already exists when I write it down resp create it. Maybe the OP just wanted to know if an HTTP-Resource represented by a HTTP-URL would be accessible for him, but that was not what the OP asked. ;-) – Michael Konietzka Nov 14 '10 at 17:37
-
@Michael Konietzka from the comment of OP in the same thread it is clear that he wants to ask about HTTP protocol only, he has asked it globally actually – jmj Nov 14 '10 at 17:39
-
@org.life.java: My last comment contained some ironic note based on my sunday evening mood, which was lost when transporting via HTTP. ;-) – Michael Konietzka Nov 14 '10 at 18:03
2
You could try HTTP's HEAD
method to see if the server returns a status code of 200
on a particular URL and act accordingly.
See http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol and scroll down to the Request Methods.

aefxx
- 24,835
- 6
- 45
- 55
0
You can make a connection, get the input stream back, and check for null.

duffymo
- 305,152
- 44
- 369
- 561
-3
I used this bash script for check urls, but need put all files in a file "urls.csv"
#!/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

ggerman
- 21
- 5