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

it2024-01-19  62

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);

 

最新回复(0)