HttpClient 模拟浏览器获取数据

HTTP协议:https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol

HttpCore: http://hc.apache.org/

maven

    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.5.2</version>
    </dependency> 

代码如下,解释在代码中

package com.oak.spider;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class HC {
    public static void main(String args[]) throws  Exception{
        //创建一个浏览器
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //构建请求
        HttpPost post = new HttpPost("http://zhangguoli.cn");
        post.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:39.0) Gecko/20100101 Firefox/39.0");
        //执行请求,获取返回数据
        CloseableHttpResponse response = httpClient.execute(post);
        //处理返回数据
        Header[] headers = response.getAllHeaders();//头部信息
        HttpEntity httpEntity = response.getEntity();//实体
        /*
        *    处理业务逻辑
        */
        //释放实体,关闭响应,关闭浏览器
        EntityUtils.consume(httpEntity);
        response.close();
        httpClient.close();
    }
}

标签: HttpClient

相关文章推荐: