Dinesh
Dinesh

Reputation: 16436

Checking internet connectivity of a system using Java

I wrote a method which will help us finding whether the system using the proxy to connect with internet which is as follows,

try {

    System.out.println("Checking internet connection availability.....");
    URL u = new URL("http://www.google.com/");
    HttpURLConnection uc = (HttpURLConnection) u.openConnection();
    uc.setReadTimeout(1);//I have tried this without timeout and with it too. But it didnt work
    System.out.println(uc.getResponseCode());
} catch (Exception ex) {
    System.out.println("Unable to connect to internet without proxy.....");
    System.out.println("Checking for any proxy settings from the PC");
    System.setProperty("java.net.useSystemProxies", "true");
    try {
        System.setProperty("java.net.useSystemProxies", "true");
        URL u = new URL("http://www.google.com/");
        HttpURLConnection uc = (HttpURLConnection) u.openConnection();
        System.out.println(uc.getResponseCode());
        System.out.println("Internet connection available");
    } catch (Exception e) {
        System.out.println("Internet connection not available :(");
    }
}

Initially, I am trying to open the URL connection without proxy (assuming the system don't have proxy to connect with internet).I have set the timeout to 1 ms. Trying to get the response code from the site. If any error occurs means (like timeout), then in the catch block i am trying to connect to internet with system's proxy, by means of setting the useSystemProxies to true. But even after that also, I am not able to get the response from the site.

I am using a system with proxy settings.

I have tried the following too in the catch block

Proxy next = ProxySelector.getDefault().select(new URI("http://www.google.com/")).iterator().next();
if (next.address() != null) {
    System.out.println("Detecting Proxy configurations.....");
    String proxy = next.address().toString();
    String proxyHost = proxy.substring(0, proxy.indexOf(":"));
    String proxyPort = proxy.substring(proxy.indexOf(":") + 1);
    System.out.println("Proxy Configuration : " + proxyHost + " @ " + proxyPort);
}

The above block of code also is not working. Can anyone help me out with this?

Upvotes: 1

Views: 1086

Answers (2)

shazin
shazin

Reputation: 21923

In your first code snippet in the catch block try setting the following codes.

System.setProperty("http.proxyHost", "Proxy host");
System.setProperty("http.proxyPort", "Proxy Port");

Seems like your proxy is registered as system proxy and not visible to the JVM.

Upvotes: 0

Achintya Jha
Achintya Jha

Reputation: 12843

InetAddress.isReachable

Test whether that address is reachable. Best effort is made by the implementation to try to reach the host, but firewalls and server configuration may block requests resulting in a unreachable status while some specific ports may be accessible. A typical implementation will use ICMP ECHO REQUESTs if the privilege can be obtained, otherwise it will try to establish a TCP connection on port 7 (Echo) of the destination host. The timeout value, in milliseconds, indicates the maximum amount of time the try should take. If the operation times out before getting an answer, the host is deemed unreachable. A negative value will result in an IllegalArgumentException being thrown.

Upvotes: 1

Related Questions