HttpClient总结

HttpClient不是浏览器,但可以模拟浏览器发送请求,主要作用:

  1. 爬取网页(Http,Https)内容
  2. 使用get/post方式发送请求,post可以是表单提交或json提交

依赖的jar包

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.7</version>
</dependency>
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpcore</artifactId>
    <version>4.4.7</version>
</dependency>

使用httpClient的步骤为:

创建httpClient对象-->创建httpGet-->执行get/post请求-->返回HTTPEntity-->通过EntityUtil取出响应内容。

1、get方式

//get方式请求
public static String get(String url) throws IOException {
//        CloseableHttpClient httpClient = HttpClients.createDefault();
    //建造者模式
    CloseableHttpClient httpClient = HttpClientBuilder.create().build();
    HttpGet httpGet = new HttpGet(url);
    CloseableHttpResponse response = null;
    response = httpClient.execute(httpGet);
    HttpEntity entity = response.getEntity();
    String html = EntityUtils.toString(entity);
    response.close();
    httpClient.close();
    return html;
}
//请求方式
get("http://www.chery.cn/vehicles/tiggo8#page-1");

2、表单提交

/**
 * 表单提交
 *
 * @param url
 * @return
 */
public static String post(String url) throws Exception {
    //和上面一样,也可以通过build创建
    CloseableHttpClient httpClient = HttpClients.createDefault();
    HttpPost httpPost = new HttpPost(url);
    CloseableHttpResponse response = null;
    List<NameValuePair> list = Lists.newArrayList();
    list.add(new BasicNameValuePair("name", "张三"));
    list.add(new BasicNameValuePair("pwd", "123321"));
    log.info(JSONObject.toJSONString(list));
    //需要把表单包装到entity中
    StringEntity formEntity = new UrlEncodedFormEntity(list, "UTF-8");
    httpPost.setEntity(formEntity);

    //设置请求和传输超时时间
    RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(3000).setConnectTimeout(3000).build();
    httpPost.setConfig(requestConfig);

    response = httpClient.execute(httpPost);
    HttpEntity entity = response.getEntity();

    String result = EntityUtils.toString(entity, "UTF-8");
    response.close();
    httpClient.close();
    return result;
}

3、json方式发送post请求

/**
 * json参数提交
 *
 * @param url
 * @return
 * @throws Exception
 */
public static String httpPostWithJSON(String url) throws Exception {

    CloseableHttpClient client = HttpClients.createDefault();
    HttpPost httpPost = new HttpPost(url);
    String respContent = null;

    //封装json参数
    //{"endpoint":"abd.cn-beijing.abc.com","reportData":{"metricName":"test","groupId":"101","values":{"value":10.5},"dimensions":{"sampleName1":"value1","sampleName2":"value2"}},"acckey":"something","secret":"something"}
    JSONObject jsonParam = new JSONObject();
    jsonParam.put("endpoint", "abd.cn-beijing.abc.com");
    jsonParam.put("acckey", "something");
    jsonParam.put("secret", "something");

    JSONObject paramVar = new JSONObject();
    paramVar.put("groupId", "101");
    paramVar.put("metricName", "test");
    paramVar.put("groupId", "101");

    JSONObject dimensions = new JSONObject();
    dimensions.put("sampleName1", "value1");
    dimensions.put("sampleName2", "value2");
    paramVar.put("dimensions", dimensions);

    JSONObject values = new JSONObject();
    values.put("value", 10.5);
    paramVar.put("values", values);

    jsonParam.put("reportData", paramVar);
    //解决中文乱码问题
    StringEntity entity = new StringEntity(jsonParam.toString(), "UTF-8");
    entity.setContentType("application/json");
    httpPost.setEntity(entity);
    log.info(JSONObject.toJSONString(jsonParam));

    HttpResponse resp = client.execute(httpPost);
    if (resp.getStatusLine().getStatusCode() == 200) {
        HttpEntity he = resp.getEntity();
        respContent = EntityUtils.toString(he, "UTF-8");
    }
    return respContent;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值