httpClient检测url地址的状态

httpClient检测url地址的状态


import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

import java.io.IOException;

/**
 * 检测url地址的状态
 * 需要用到httpclient-4.5.5.jar、httpcore-4.4.9.jar的jar包支持
 * Created by 张勇波 on 18-3-31.
 */
public class NetState {
    public static int urlStatusLine(String url) {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //创建httpGet实例
        HttpGet httpGet = new HttpGet(url);
        CloseableHttpResponse response = null;
        try {
            response = httpClient.execute(httpGet);
            if (response != null) {
                return response.getStatusLine().getStatusCode();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (response != null) {
                    response.close();
                }
                if (httpClient != null) {
                    httpClient.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return 0;
    }

    public static void main(String[] args) {
        //String url = "https://www.oschina.net/code/snippet_1434_1158";//403
        //String url="http://hc.apache.org/downloads.cgi";//200
        String url = "https://www.cnblogs.com/exmyth/p/6432261.html";//200
        System.out.println(urlStatusLine(url));
    }
}

 上一篇
event is not defined event is not defined
event is not definedFirefox下ReferenceError: event is not defined得到触发事件的元素引用在IE浏览器下是:event.srcElement,在FF浏览器下则是:event.tar
2019-08-19
下一篇 
http跨域设置 http跨域设置
http跨域设置开发http接口需要进行跨域支持:在被请求的Response header中加入 // 指定允许其他域名访问 response.setHeader("Access-Control-Allow-Origin", "*");
2019-08-19
  目录