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 );
Ht开发者_如何学JAVAtpURLConnection 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?
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 ");
}
It could be that the servers are up and running (responding to ping), but that no HTTP server is listening on that port.
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.
精彩评论