It works in this way, It return code 200 if you are connected to Internet else return code is 401,404 etc.,
/*********************************************/
import java.net.*;
import java.io.*;
import javax.swing.*;
import java.awt.*;
public class SourceViewer {
public static void main(
String[] args){
try{
URL u = new URL("http://www.yahoo.com/");
System.out.println("Host...."+u.getHost());
HttpURLConnection uc = (HttpURLConnection) u.openConnection();
int code = uc.getResponseCode();
System.out.println("Return Code....."+code);
}
catch(IOException e){
e.printStackTrace();
System.err.println(e);
}
}
}
/********************************************************/