编程语言
首页 > 编程语言> > Java探测URL页面是否正常

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;
    }
}

 

标签:status,Exception,Java,URL,oc,public,HttpURLConnection,页面
来源: https://www.cnblogs.com/cjzzz/p/15923526.html