java 判断网络通断

private static boolean isInternetOK(String[] urls) {
        System.setProperty("http.proxyHost", "192.168.0.100");
        System.setProperty("http.proxyPort", "9999");
        System.setProperty("https.proxyHost", "192.168.0.100");
        System.setProperty("https.proxyPort", "9999");
        
        for (String url : urls) {
            try {
                HttpURLConnection urlConnection = (HttpURLConnection) new URL(url).openConnection();
                urlConnection.setRequestMethod("HEAD");
                urlConnection.setConnectTimeout(5000); // 设置连接超时时间为5秒钟
                int statusCode = urlConnection.getResponseCode();
                if (statusCode == 200)
                    return true;
            } catch (Exception e) {

            }
        }
        return false;
    }

 

posted on 2023-05-18 10:20  空明流光  阅读(82)  评论(0)    收藏  举报

导航