Java(16):httpclient(CloseableHttpClient) http的Get和Post请求的封装(2)

主要实现4个封装方法:

1.将不同类型的请求如 GET和POST封装起来

GET请求(不带参数的GET/带参数的GET/不带token的GET/带token的GET),

POST请求(不带参数的POST/带参数的POST/带token的POST)

2.请求参数进行封装

3.释放资源进行封装

4.响应结果的封装

前提:引用httpclient

使用的是Maven,则可以添加以下依赖项,它将包括使用Apache HttpClient所需的所有其他依赖项。

3.1.引用maven依赖使用httpclient.jar
        <dependency>

            <groupId>org.apache.httpcomponents</groupId>

            <artifactId>httpclient</artifactId>

            <version>4.5.3</version>

        </dependency>

参考:https://blog.csdn.net/fen_fen/article/details/120023317

封装详细信息:

1.将不同类型的请求如 get,不带参数的get,带参数的get,不带参数的post,带参数的post封装起来

1.1 GET方法

  /**

     * 发送HttpGet请求,不带请求头和请求参数

     * @param url

     * @return

     */

    public static HttpClientResult doGet(String url) throws Exception {

        return doGet(url,null,null);

    }


    /**

     * 发送HttpGet请求,带请求头和请求参数

     * @param url

     * @param token

     * @return

     */

    public static HttpClientResult doGet(String url, Map<String,String> params, String token) throws Exception{

   
        CloseableHttpClient httpClient = null;
        CloseableHttpResponse httpResponse = null;
        String result = null;
        try {
            //1.创建httpClient对象
            httpClient = HttpClients.createDefault();
            // 创建访问的地址
            URIBuilder uriBuilder = new URIBu
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,以下是使用HttpClient5发送POST和GET请求Java代码示例: ```java 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.HttpClients; import org.apache.hc.core5.http.ClassicHttpResponse; import org.apache.hc.core5.http.ContentType; import org.apache.hc.core5.http.io.entity.StringEntity; import java.io.IOException; import java.net.URI; public class HttpClientUtils { /** * 发送GET请求 * * @param url 请求的URL * @return 响应字符串 * @throws IOException */ public static String sendGetRequest(String url) throws IOException { try (CloseableHttpClient httpClient = HttpClients.createDefault()) { HttpGet httpGet = new HttpGet(url); ClassicHttpResponse response = httpClient.execute(httpGet); return response.getEntity().toString(); } } /** * 发送POST请求 * * @param url 请求的URL * @param params 请求参数(JSON格式) * @return 响应字符串 * @throws IOException */ public static String sendPostRequest(String url, String params) throws IOException { try (CloseableHttpClient httpClient = HttpClients.createDefault()) { HttpPost httpPost = new HttpPost(URI.create(url)); StringEntity requestEntity = new StringEntity(params, ContentType.APPLICATION_JSON); httpPost.setEntity(requestEntity); ClassicHttpResponse response = httpClient.execute(httpPost); return response.getEntity().toString(); } } } ``` 这里我们使用了Apache HttpClient5库来发送请求,通过封装成方法,可以方便地在其他地方调用。注意,在使用完CloseableHttpClient后,一定要关闭它,否则可能会导致连接泄露和资源浪费。在上面的示例中,我们使用了try-with-resources语法糖来自动关闭CloseableHttpClient

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

宁宁可可

您的鼓励是我创作的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值