I'm developing a web-program that needs to check a whole bounch of potentially faulty http-links. Now, I have found a way of doing this, the only problem is that it relies on try-catch, not in it self horribly bad, but the time it takes to check the links is extremely variable. It ranges from about 20k ns to 1,8M ns for links that works. For links that is not working propperly (eg. a link that has a server (ip) but is not a real http) the time it takes the code to "figure out" that an exception should be thrown is upwards of 30 seconds! As you might tell, this is not very practical.

private boolean tryLink(String linkToTest) {
		//Open connectino to link and return true if link works, false otherwise
		long end, start = System.nanoTime();
		URL myurl;
		try {
			myurl = new URL(linkToTest);
			HttpURLConnection conn = (HttpURLConnection)myurl.openConnection();
			conn.setRequestMethod("GET");
		} catch (Exception e) {
			end = System.nanoTime();
			if(end-start>50000){
				System.out.println("TryLink time: "+(end-start)+". Link not ok");
				System.out.println("Slow link was: "+linkToTest);
			}
			return false;
		}
		end = System.nanoTime();
		if(end-start>50000){
			System.out.println("TryLink time: "+(end-start)+". Link OK");
			System.out.println("Slow link was: "+linkToTest);
		}
		return true;
	}

Is there a more elegant way of testing a http link? Any and all help is much appreciated!

Have a time limit that you are willing to wait for the server.
Have the test fail if the server doesn't respond in that time limit.
That could return false values if the server does take longer than your limit.

Use ping to test if the server exists.

Now i have tried to use ping, but it seems it is much slower to use this ping method (isReachable() in InetAddress and another more scetchy method).I think the best bet is to put a time limit on the thing. Though the ping methods might be more consistant, they perform so much less on accually working links that I don't see the benefit.

Any idea on a resonable time limit? I got a good connection on my current location, basicly connected straight into the mainframe, so what would be the longes delay to where ever? 5 sec maybe?