天天看點

3.2.4 HttpClient設定頭資訊

//程式3-15
public class HttpClientSetHeader {
    public static void main(String[] args) throws Exception {
        //初始化httpclient
        HttpClient httpClient = HttpClients.custom().build();
        //使用的請求方法
        HttpGet httpget = new HttpGet("https://searchcustomerexperience.techtarget.com/info/news");
        //請求頭配置
        httpget.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
        httpget.setHeader("Accept-Encoding", "gzip, deflate");
        httpget.setHeader("Accept-Language", "zh-CN,zh;q=0.9");
        httpget.setHeader("Cache-Control", "max-age=0");
        httpget.setHeader("Host", "searchcustomerexperience.techtarget.com");
        httpget.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.108 Safari/537.36"); //這項内容很重要
        //發出get請求
        HttpResponse response = httpClient.execute(httpget);
        //擷取響應狀态碼
        int code = response.getStatusLine().getStatusCode();
        //擷取網頁内容流
        HttpEntity httpEntity = response.getEntity();
        //以字元串的形式(需設定編碼)
        String entity = EntityUtils.toString(httpEntity, "gbk");
        //輸出所獲得的的内容
        System.out.println(code + "\n" + entity);
        //關閉内容流
        EntityUtils.consume(httpEntity);
    }
}      
//程式3-16
public class HttpclientSetHeader {
    public static void main(String[] args) throws Exception {
        //通過集合封裝頭資訊
        List<Header> headerList = new ArrayList<Header>();
        headerList.add(new BasicHeader(HttpHeaders.ACCEPT, "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8"));
        headerList.add(new BasicHeader(HttpHeaders.USER_AGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.108 Safari/537.36"));
        headerList.add(new BasicHeader(HttpHeaders.ACCEPT_ENCODING, "gzip, deflate"));
        headerList.add(new BasicHeader(HttpHeaders.CACHE_CONTROL, "max-age=0"));
        headerList.add(new BasicHeader(HttpHeaders.CONNECTION, "keep-alive"));
        headerList.add(new BasicHeader(HttpHeaders.ACCEPT_LANGUAGE, "zh-CN,zh;q=0.9"));
        headerList.add(new BasicHeader(HttpHeaders.HOST, "searchcustomerexperience.techtarget.com"));
        //構造自定義的HttpClient對象
        HttpClient httpClient = HttpClients.custom().setDefaultHeaders(headerList).build();
        //使用的請求方法
        HttpGet httpget = new HttpGet("https://searchcustomerexperience.techtarget.com/info/news");
        //發出get請求并擷取結果
        HttpResponse response = httpClient.execute(httpget);
        //擷取響應狀态碼
        int code = response.getStatusLine().getStatusCode();
        //擷取網頁内容流
        HttpEntity httpEntity = response.getEntity();
        //以字元串的形式(需設定編碼)
        String entity = EntityUtils.toString(httpEntity, "gbk");
        //輸出所獲得的的内容
        System.out.println(code + "\n" + entity);
        //關閉内容流
        EntityUtils.consume(httpEntity);
    }
}