RestTemplate和httpClient发送post区别(get请求)

public void postHttp() throws UnsupportedEncodingException {
        User user = new User();
        user.setUid(1);
        user.setName("张三");
        String jsonParam = JSONObject.toJSONString(user);
        log.info("jsonParam:{}", jsonParam);
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        List<NameValuePair> nameValuePairList = new ArrayList<>();
        nameValuePairList.add(new BasicNameValuePair("authKey", "authKey"));
        nameValuePairList.add(new BasicNameValuePair("initData", JSONObject.toJSONString(jsonParam)));
        nameValuePairList.add(new BasicNameValuePair("title", "title"));
        //创建httpPost,url代表你需要访问的远程地址
        HttpPost httpPost = new HttpPost("url");
        // 设置提交方式
        httpPost.addHeader("Content-type", "application/x-www-form-urlencoded");
        // 设置参数
        httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairList));

        CloseableHttpResponse httpResponse = null;
        try {
            httpResponse = httpClient.execute(httpPost);
            //取实体
            HttpEntity httpEntity = httpResponse.getEntity();
            if (httpEntity != null) {
                log.info("响应内容:{}", EntityUtils.toString(httpEntity, "utf-8"));
            }
        } catch (Exception e) {

        } finally {
            try {
                if (httpClient != null) {
                    httpClient.close();
                }
                if (httpResponse != null) {
                    httpResponse.close();
                }
            } catch (Exception e) {

            }
        }

        /**
         * restTemplate
         */
        //创建请求头
        HttpHeaders headers = new HttpHeaders();
        User user1 = new User();
        user1.setUid(1);
        user1.setName("张三");
        String jsonParam1 = JSONObject.toJSONString(user1);

        MultiValueMap<String, Object> postParameters = new LinkedMultiValueMap<>();
        postParameters.add("title", "title");
        postParameters.add("authKey", "authKey");
        postParameters.add("initData", JSONObject.toJSONString(jsonParam1));
        org.springframework.http.HttpEntity<MultiValueMap<String, Object>> httpEntity
            = new org.springframework.http.HttpEntity<>(postParameters, headers);

        String string = restTemplate.postForObject("url", httpEntity, String.class);
        log.info("响应内容:{}", string);
    }



get

  ResponseEntity<ProvinceAreaSiteVO> response = restTemplate.getForEntity
                (sqUrl + "/motor-service/tms/province/area/center/site/get", ProvinceAreaSiteVO.class);
        ProvinceAreaSiteVO provinceAreaSiteResultVOList =
                BeanUtil.copyProperties(response.getBody(), ProvinceAreaSiteVO.class);


/**
 * 省区
 * @author keying
 */
@Data
public class ProvinceAreaSiteVO {

    private Boolean status = false;

    private String message;

    private String statusCode;

    private List<ProvinceAreaSiteResultVO> result;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

后端从入门到精通

你的鼓励是我最大的动力~

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

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

打赏作者

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

抵扣说明:

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

余额充值