0

I have many URLs want to create a program that will see if the URL can be connected to and loaded.

I was thinking I could try downloading the source, but if it isn't a site then it wont download it. Is there a more efficient way of doing this?

EDIT: I'm going to use a HttpURLConnection and was wondering what method is fastest.

HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");

Is post or get better, and why?

Aaron
  • 992
  • 3
  • 15
  • 33
user2526311
  • 1,004
  • 2
  • 10
  • 19

1 Answers1

0

If you just want to check syntax, the URL's constructor will address it. If you want to check for connectivity:

HttpURLConnection connection = (HttpURLConnection)url.openConnection();
try {
    connection.setMethod("GET");
    connection.connect();
} catch (IOException e) {
try {
    connection.setMethod("POST");
    connection.connect();
} catch (IOException e) {
try {
    connection.setMethod("HEAD");
    connection.connect();
} catch (IOException e) { try {
    connection.setMethod("DELETE");
    connection.connect();
}}}} catch (IOException e) {
// failed
}
hd1
  • 33,938
  • 5
  • 80
  • 91