Tuesday, June 05, 2007

URLPing - A Java Utility to Ping a URL

Recently there was a need at work to programmatically check the status of a URL. The requirement was to test if a specified URL returned a HTTP Status Code 200 OK or otherwise. URLPing, the utility that I wrote has been hosted on Google Code and is available for download and reuse. The utility uses the Apache Jakarta Commons HttpClient component.

URLPing is the main class. This class provides two methods ping(String url) and ping(String url, int timeout). The first method accepts a URL to ping and defaults to a timeout of 30 seconds. The second method accepts a timeout interval in seconds in addition to the URL to ping.

URLPing urlPing = new URLPing();
PingResponse pingResponse = urlPing.ping("http://www.techvj.com");
        
assertEquals(pingResponse.getResponseStatus(), ResponseStatus.OKAY);

The response to the ping method is a PingReponse object. This contains a ResponseStatus enum with values of either OKAY, ERROR or TIMEOUT depending on the outcome of the ping. It also contains a responseCode property which contains the response code of the HTTP request.

1 comment:

Anonymous said...

Hi,
I would like to know if there's a way to adapt your code for https? Thanks in advance.
Arnaud