spring boot 中使用commons-httpclient发送post和get请求

 依赖的包

<dependency>
     <groupId>commons-httpclient</groupId>
     <artifactId>commons-httpclient</artifactId>
     <version>3.1</version>
 </dependency>
<dependency>
     <groupId>com.alibaba</groupId>
     <artifactId>fastjson</artifactId>
     <version>1.2.70</version>
</dependency>
<dependency>
     <groupId>org.projectlombok</groupId>
     <artifactId>lombok</artifactId>
     <optional>true</optional>
</dependency>

请求方法封装

package com.test.utils;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Map;

/**
 * @Auther: Xu
 * @Date: 2022/11/15 - 11 - 15 - 16:52
 * @Description: com.test.utils
 * @version: 1.0
 */
@Slf4j
public class HttpClientUtils {
    public static String post(Object params,String url){
        try{
            HttpClient httpClient = new HttpClient();
            PostMethod postMethod = new PostMethod(url);

            //        postMethod.addRequestHeader("accept", "*/*");
            //设置Content-Type,此处根据实际情况确定
            postMethod.addRequestHeader("Content-Type", "application/json");
            //添加请求参数
            RequestEntity entity = new StringRequestEntity(JSON.toJSONString(params),"application/json","UTF-8");
            postMethod.setRequestEntity(entity);
            //http状态码
            int code = httpClient.executeMethod(postMethod);
            InputStream in = postMethod.getResponseBodyAsStream();
            //下面将stream转换为String
            StringBuffer sb = new StringBuffer();
            InputStreamReader isr = new InputStreamReader(in, "UTF-8");
            char[] b = new char[4096];
            for(int n; (n = isr.read(b)) != -1;) {
                sb.append(new String(b, 0, n));
            }
            String returnStr = sb.toString();
            httpClient = null;
            postMethod = null;
            b = null;
            return returnStr;
        }catch (Exception e){
            e.printStackTrace();
            log.error("请求异常{}",e.getMessage());
            return null;
        }
    }
    public static String get(Map<String,Object> params,String url){
        String returnStr = null;
        try{
            HttpClient httpClient = new HttpClient();
            GetMethod getMethod = new GetMethod(url);

            //设置Content-Type,此处根据实际情况确定
            getMethod.addRequestHeader("Content-Type", "application/json");
            //添加请求参数
            getMethod.setQueryString(getUrlParamsByMap(params));
            int code = httpClient.executeMethod(getMethod);
            InputStream in = getMethod.getResponseBodyAsStream();
            //下面将stream转换为String
            StringBuffer sb = new StringBuffer();
            InputStreamReader isr = new InputStreamReader(in, "UTF-8");
            char[] b = new char[4096];
            for(int n; (n = isr.read(b)) != -1;) {
                sb.append(new String(b, 0, n));
            }
            returnStr = sb.toString();
            httpClient = null;
            getMethod = null;
            b = null;
        }catch (Exception e){
            e.printStackTrace();
            log.error("请求异常{}",e.getMessage());
        }
        return returnStr;
    }

    //将map处理成?a=1&b=2的形式
    public static String getUrlParamsByMap(Map<String, Object> map) {

        if (map == null) {
            return "";
        }

        StringBuffer sb = new StringBuffer();
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            sb.append(entry.getKey() + "=" + entry.getValue());
            sb.append("&");
        }

        String s = "?"+sb.toString();

        if (s.endsWith("&")) {
            s = s.substring(0,s.length()-1);
        }
        System.out.println(s);
        return s;

    }
}

使用方式

Map<String,Object> m = new HashMap<>();
m.put("id","b");
m.put("name","e");
String s = HttpClientUtils.post(m,"http://*:10001/getdata");

Map<String,Object> m = new HashMap<>();
m.put("id","b");
m.put("name","e");
String s = HttpClientUtils.get(m,"http://*::10001/getdatb");

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

攻城狮狮

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值