RestTemplate 发出的http请求在postman测试可以但是使用RestTemplate发出的请求不行的原因

RestTemplate发出的请求如何我们请求方,参数如果存在+=这种需要转义的字符的时候,我们需要对url进行转义之后再进行请求,但是RestTemplate会默认对我们的Url进行encode编码,如果你之前进行了encode编码,restTemplate会再进行一次encode,所以会导致传过去的参数不对,我也尝试过既然restTemplate会对url进行encode编码,那url传进去之前不进行encode不就行了,我试过,还是参数不正确,所以我只能先进行encode参数,然后自己构建url传进去,代码如下:
 String type = URLEncoder.encode(encrypte);
        String open = URLEncoder.encode(openId);
        String vi = URLEncoder.encode(iv);
        String url = "localhost:8080/napi/piccwx_api/piccwxapi_tx/getMpPhone?encrypted=" + type + "&openid=" + open + "&iv=" + vi;
        URI uri = UriComponentsBuilder.fromUriString(url).build(true).toUri();
        logger.info("url:" + uri);
        ResponseEntity<String> responseEntity = restTemplate.exchange(uri, HttpMethod.POST, null, String.class);

 

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值