【Java】HttpClient 5 CloseableHttpClient使用

依赖

<dependency>
    <groupId>org.apache.httpcomponents.client5</groupId>
    <artifactId>httpclient5</artifactId>
    <version>5.2.1</version>
</dependency>

引入包

import com.alibaba.fastjson2.JSON;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.HttpEntity;
import org.apache.hc.core5.http.HttpStatus;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.http.io.entity.StringEntity;

POST 有参数

public A findXxx(String code) {
    // 定义一个A对象,用于返回
    A a= null;
    // 创建HttpClient
    CloseableHttpClient client = HttpClients.createDefault();
    // 创建HttpPost
    HttpPost httpPost = new HttpPost(url);
    log.info("HttpUrl:{}", url);
    // 设置Headers
    httpPost.setHeader("xxx", "xxx");
    // 设置参数
    Map<String, Object> params = new HashMap<>();
    params.put("xxx", "xxx");
    httpPost.setEntity(new StringEntity(JSON.toJSONString(params)));
    // 执行
    try (CloseableHttpResponse response = client.execute(httpPost)) {
        if (response.getCode() == HttpStatus.SC_OK) {
            HttpEntity httpEntity = response.getEntity();
            // 获取json字符串
            String jsonString = EntityUtils.toString(httpEntity);
            log.info("findXxxResponse:{}", jsonString);
            if (jsonString.contains("errorCode")) {
                log.error("findXxxFail:{}", jsonString);
            } else {
                // 获取json对象
                a = JSON.parseObject(jsonString, A.class);
            }
        } else {
            log.error("findXxxFail:{}", response);
        }
    } catch (Exception e) {
        log.error(ResultCode.HTTP_FAIL.message(), e);
    } finally {
        // 释放连接资源
        httpPost.reset();
    }
    return a;
}

GET 无参数

public A findXxx(String code) {
    // 定义一个A对象,用于返回
    A a= null;
    // 创建HttpClient
    CloseableHttpClient client = HttpClients.createDefault();
    // 拼接url
    String url = xxxUrl + "?xxx=xxx";
    // 创建HttpGet
    HttpGet httpGet = new HttpGet(url);
    log.info("HttpUrl:{}", url);
    // 设置Headers
    httpGet.setHeader("xxx", "xxx");
    // 执行
    try (CloseableHttpResponse response = client.execute(httpGet)) {
        if (response.getCode() == HttpStatus.SC_OK) {
            HttpEntity httpEntity = response.getEntity();
            // 获取json字符串
            String jsonString = EntityUtils.toString(httpEntity);
            log.info("findXxxResponse:{}", jsonString);
            if (jsonString.contains("errorCode")) {
                log.error("findXxxFail:{}", jsonString);
            } else {
                // 获取json对象
                a = JSON.parseObject(jsonString, A.class);
            }
        } else {
            log.error("findXxxFail:{}", response);
        }
    } catch (Exception e) {
        log.error(ResultCode.HTTP_FAIL.message(), e);
    } finally {
        // 释放连接资源
        httpGet.reset();
    }
    return a;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值