tags:

views:

60

answers:

3

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?

A: 

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.

Amir Raminfar
Could you share the ip:port or is it internal? Can you find an example that we can test?
Amir Raminfar
+1  A: 

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 ");
}
Gareth Davis
Yes. But I've to do it the port number!
Ritz
+1  A: 

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