Returns whether the host system is connected to the internet - Java Native OS

Java examples for Native OS:Browser

Description

Returns whether the host system is connected to the internet

Demo Code


//package com.java2s;

import java.io.IOException;
import java.net.HttpURLConnection;

import java.net.URL;

public class Main {
    public static void main(String[] argv) throws Exception {
        System.out.println(isConnected());
    }//from   ww  w.jav  a2  s. co m

    /**
     * Returns whether the host system is connected to the internet
     *
     * @return Whether the host system is connected to the internet
     */
    public static boolean isConnected() {
        try {
            URL url = new URL("https://google.com");
            HttpURLConnection urlConnect = (HttpURLConnection) url
                    .openConnection();
            return urlConnect.getResponseCode() == 200;
        } catch (IOException ex) {
            return false;
        }
    }
}

Related Tutorials