0

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?

1
  • you do realize that the ConnectException signals that, from the perspective of the machine this code is running on, the URL is "not up", right? If the definition of "not up" is "cannot be connected to from this machine". Commented Oct 26, 2010 at 15:06

3 Answers 3

1

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 "); } 
Sign up to request clarification or add additional context in comments.

1 Comment

Yes. But I've to do it the port number!
1

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

Comments

0

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.

1 Comment

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.