Possible Duplicate:
Detect internet Connection using Java
How to check if internet connection is present in java?
Following is my code to check whether PC has internet connectivity or not:
try{
URL url = new URL("http://www.google.co.in");
URLConnection connection = url.openConnection();
connected = true;
}
catch(IOException ioe){
connected = false;
ioe.printStackTrace();
}
I want to know whether this a reliable and best method? Whenever there is no internet connectivity, an acception would be thrown and the value of boolean variable connected equals false
.