Java探测URL页面是否正常

使用Java实现探测URL页面是否正常


import java.net.HttpURLConnection;
import java.net.URL;

public class service {
    public static void main(String[] args) throws Exception {
        System.out.println(service.testWsdlConnection("https://www.baidu1.com/?tn=02003390_2_hao_pg"));
    }

    public static int testWsdlConnection(String address) throws Exception {
        int status = 404;
        try {
            URL urlObj = new URL(address);
            HttpURLConnection oc = (HttpURLConnection) urlObj.openConnection();
            oc.setUseCaches(false);
            oc.setConnectTimeout(3000); // 设置超时时间
            status = oc.getResponseCode();// 请求状态
            if (200 == status) {
                // 200是请求地址顺利连通。。
                return status;
            }
        } catch (Exception e) {
            //e.printStackTrace();
            //throw e;
        }
        return status;
    }
}

 

posted @ 2022-02-22 15:49  RFAA  阅读(511)  评论(0编辑  收藏  举报