史上最完善的判断URL是否可访问工具类

 1.URLUtils工具类

@Slf4j
public class URLUtils {
    private static final String URL_PREFIX_INSECURITY = "http://";
    private static final String URL_PREFIX_SECURITY = "https://";

    /*** 建立连接http连接*/
    private static boolean connect(String httpURL, int timeOutMillSeconds) {
        URL url;
        try {
            url = new URL(httpURL);
            URLConnection co = url.openConnection();
            co.setConnectTimeout(timeOutMillSeconds);
            co.connect();
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    /*** 判断加了http或HTTPS的连接是否可访问*/
    private static boolean isAvailable(String httpURL, int timeOutMillSeconds) {
        return connect(httpURL, timeOutMillSeconds);
    }

    /*** 判断没加http或HTTPS的连接是否可访问*/
    private static boolean isAvailable(String noHttpURL, int timeOutMillSeconds, boolean isSecure) {
        String httpURL;
        if (isSecure) {
            httpURL = URL_PREFIX_SECURITY + noHttpURL;
        } else {
            httpURL = URL_PREFIX_INSECURITY + noHttpURL;
        }
        return connect(httpURL, timeOutMillSeconds);
    }

    /**
     * 检查URL是否可访问
     *
     * @param url                访问URL
     * @param timeOutMillSeconds 超时时间
     * @param checkType          检查类型https|http|http or https 默认传2
     * @return
     */
    public static boolean isAvailable(String url, int timeOutMillSeconds, int checkType) {
        long lo = System.currentTimeMillis();
        boolean flag = false;
        if (url.indexOf(URL_PREFIX_SECURITY) == 0 || url.indexOf(URL_PREFIX_INSECURITY) == 0) {
            flag = isAvailable(url, timeOutMillSeconds);
        } else {
            if (checkType == 0) {
                //加https是否可用
                flag = isAvailable(url, timeOutMillSeconds, true);
            } else if (checkType == 1) {
                //加http是否可用
                flag = isAvailable(url, timeOutMillSeconds, false);
            } else if (checkType == 2) {
                //加http或https是否可用
                boolean availableSecurity = isAvailable(url, timeOutMillSeconds, true);
                boolean availableInSecurity = isAvailable(url, timeOutMillSeconds, false);
                if (availableSecurity || availableInSecurity) {
                    flag = true;
                } else {
                    flag = false;
                }
            }
        }
        if (flag) {
            log.info(">>>>>>>>>>>{}连接成功,连接时间:{}ms", url, (System.currentTimeMillis() - lo));
        } else {
            log.error(">>>>>>>>>>>{}连接不存在,超时时间:{}ms", url, (System.currentTimeMillis() - lo));
        }
        return flag;
    }

    public static void main(String[] args) {
        isAvailable("http://baidu.com", 2000, 2);
        isAvailable("baidu.com", 2000, 2);
        isAvailable("cannotVisit", 2000, 2);
    }

}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值