完整的Httpclient工具类发送带params和Header的post和get请求

1.HttpClient工具类

public class HttpClientUtils {

	/**
	带请求头的get请求
	**/
   public static String doGet(String url) throws IOException {
        CloseableHttpClient client=null;
        CloseableHttpResponse response=null;
        try {
            //1.创建httplient
             client = HttpClients.createDefault();
            //2.创建httpget
            HttpGet httpGet = new HttpGet(url);
            //3.添加请求头
            httpGet.setHeader(new BasicHeader("k", "v"));
            //4.执行请求
             response = client.execute(httpGet);
            //5.获取响应头
            int statusCode = response.getStatusLine().getStatusCode();
            if (200 == statusCode) {
             //6.获取响应实体
                return EntityUtils.toString(response.getEntity(), "utf-8");
            }
        } finally {
            //关流相关
            response.close();
            client.close();
        }
        return null;
    }

/**
带实体参数和请求头的post请求
**/
   public static String doPost(String url) throws IOException {
        CloseableHttpClient client=null;
        CloseableHttpResponse response=null;
        try {
            //1.创建httplient
             client = HttpClients.createDefault();
            //2.创建httppost
            HttpPost httpPost = new HttpPost(url);
            //3.添加请求头,和请求体
            httpPost.setHeader(new BasicHeader("Content-Type", "application/json;charset=utf-8"));
            String jsonParam="{\"username\":\"123456\",\"password\":\"123456\"}";
            httpPost.setEntity(new StringEntity(jsonParam, ContentType.APPLICATION_JSON));
            //4.执行请求
             response = client.execute(httpPost);
            //5.获取响应头
            int statusCode = response.getStatusLine().getStatusCode();
            if (200 == statusCode) {
                //6.获取响应实体
                return EntityUtils.toString(response.getEntity(), "utf-8");
            }
        }finally {
            //关流相关
            response.close();
            client.close();
        }
        return null;
    }

2.注意点

注意点1:

post请求添加请求体参数:httpPost.setEntity(new StringEntity(jsonParam, ContentType.APPLICATION_JSON));
	
	这儿默认的请求体类型为:httpPost.setEntity(new StringEntity(jsonParam)); text/plain:普通文本类型
用哪种请求体类型取决于接受参数体Controller

text/plain  vs  application/json

//这种类型接受的请求体类型applicatin/json
 @RequestMapping("back")
    public String goToBackIndex(@RequestBody Map<String,String> jsonpObject){
        System.out.println("跳到后台添加首页");
        return "addProduct";
    }
 @RequestMapping("back")
    public String goToBackIndex(@RequestBody JsonObject jsonpObject){
        System.out.println("跳到后台添加首页");
        return "addProduct";
    }
  @RequestMapping("back")
    public String goToBackIndex(@RequestBody User jsonpObject){
        System.out.println("跳到后台添加首页");
        return "addProduct";
    }
//这种类型接受的请求体类型text/plain
 @RequestMapping("back")
    public String goToBackIndex(@RequestBody String jsonpObject){
        System.out.println("跳到后台添加首页");
        return "addProduct";
    }
    
注意点21.发送json数据时,要么用 JSONObject.toJSONString(object)将对象直接转为json字符串
	2.要么在自定义json字符串的时候,要用转义字符:如:
 	String jsonParam="{\"username\":\"123456\",\"password\":\"123456\"}";

//这种形式无法解析。
String jsonParam="{'username':'123456','password':'123456'}";

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值