Rites
Rites

Reputation: 2332

Checking the url is up or not?

I need to check whether a particular url is up or not. The format of the url is like

http://IP:port

When I use java.net.URL class then I get java.net.SocketException or java.net.ConnectException.

When i ping these IPs, I find them up then why java is not able to recognise them?

The code I'm writing is

URL url = new URL( urlString );
HttpURLConnection httpConn =  (HttpURLConnection)url.openConnection();
httpConn.setInstanceFollowRedirects( false );
httpConn.setRequestMethod( "HEAD" ); 
httpConn.connect();

Port number is must to use! How can I check them using java?

Upvotes: 0

Views: 2414

Answers (3)

Mew
Mew

Reputation: 1049

It could be that the servers are up and running (responding to ping), but that no HTTP server is listening on that port.

Upvotes: 1

Gareth Davis
Gareth Davis

Reputation: 28059

Works just fine from here:

URL url = new URL( "http://google.com/" );
HttpURLConnection httpConn =  (HttpURLConnection)url.openConnection();
httpConn.setInstanceFollowRedirects( false );
httpConn.setRequestMethod( "HEAD" ); 
httpConn.connect();

System.out.println( "google.com : " + httpConn.getResponseCode());

or for failure:

URL url = new URL( "http://google.com:666/" );
HttpURLConnection httpConn =  (HttpURLConnection)url.openConnection();
httpConn.setInstanceFollowRedirects( false );
httpConn.setRequestMethod( "HEAD" ); 
try{
    httpConn.connect();
     System.out.println( "google.com : " + httpConn.getResponseCode());
}catch(java.net.ConnectException e){
     System.out.println( "google.com:666 is down ");
}

Upvotes: 1

Amir Raminfar
Amir Raminfar

Reputation: 34150

Maybe there is something else going on but I found this page to have it doing exactly what you want: http://www.rgagnon.com/javadetails/java-0059.html

They don't have port numbers in the url though.

Upvotes: 0

Related Questions