Java爬虫小白 HttpClient POST 带参数请求

直接上代码

public static void main(String[] args) throws Exception {
        //创建浏览器
        CloseableHttpClient httpClient = HttpClients.createDefault();

        //输入访问的地址
        HttpPost httpPost = new HttpPost("https://movie.douban.com/subject/26858510/");

        //创建参数
        List prams = new ArrayList<>();

        prams.add(new BasicNameValuePair("tag","热门"));
        prams.add(new BasicNameValuePair("from","gaia"));

        //将list参数 转换成post请求需要的嗯entity
        UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(prams);

        //设置post参数

        httpPost.setEntity(formEntity);

        //点击回车,访问地址,拿到返回数据
        CloseableHttpResponse httpResponse = httpClient.execute(httpPost);

        //解析数据
        if (httpResponse.getStatusLine().getStatusCode() == 200) {
            //获得返回的entity
            HttpEntity entity = httpResponse.getEntity();
            //将entity转换成字符串
            System.out.println(EntityUtils.toString(entity, "utf-8"));
        }
    }

你可能感兴趣的:(爬虫学习)