java判断http地址是否连通

https://www.cnblogs.com/chenggang816/p/10214950.html

 

    private boolean isOk(String url) {
        if(StrUtil.isEmpty(url)) return false;
        try {
            URL netUrl = new URL(url);
            HttpURLConnection connection = (HttpURLConnection) netUrl.openConnection();
            connection.setConnectTimeout(3000); //连接主机超时时间ms
            connection.setReadTimeout(3000); //从主机读取数据超时时间ms
            if (HttpURLConnection.HTTP_OK == connection.getResponseCode()) {
                System.out.println("网络联通!");
                return true;
            }
        } catch (IOException e) {
            log.error("连接不通", e.getMessage());
            return false;
        }
        return false;
    }

 

posted @ 2021-12-23 11:05  Peter.Jones  阅读(572)  评论(0)    收藏  举报