最初代码
String fileId = "fileId";
String token = "token";
String thirdToken = URLEncoder.encode(token, "UTF-8");
String baseStr = "baseStr";
String oauthSignature = "oauthSignature";
log.info("baseStr:{};oauthSignature:{}", baseStr, oauthSignature);
HttpHeaders headers = new HttpHeaders();
headers.add("baseStr", baseStr);
headers.add("oauthSignature", oauthSignature);
HttpEntity<String> httpEntity = new HttpEntity<String>(headers);
String url = params.getTokenCheckUrl() + "?fileId=" + fileId + "&token=" + thirdToken;
ResponseEntity<String> rsStr = restTemplate.getForEntity(url, String.class, httpEntity);
返回报错
调用--token验证,返回结果=rsStr:<200,{"resultCode":"1003","resultMsg":"账号不存在"}>
打开postman
?研究了一下午,发现实际问题是:
RestTemplate发送请求时 自动对参数进行urlencode的问题
修正后代码
String fileId = "fileId";
String token = "token";
String thirdToken = URLEncoder.encode(token, "UTF-8");
String baseStr = "baseStr";
String oauthSignature = "oauthSignature";
log.info("baseStr:{};oauthSignature:{}", baseStr, oauthSignature);
HttpHeaders headers = new HttpHeaders();
headers.add("baseStr", baseStr);
headers.add("oauthSignature", oauthSignature);
HttpEntity<String> httpEntity = new HttpEntity<String>(headers);
String url = params.getTokenCheckUrl() + "?fileId=" + fileId + "&token=" + thirdToken;
// 通过UriComponentsBuilder创建URI对象,这样RestTemplate不会自动进行urlencode
UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromHttpUrl(url);
URI uri = uriComponentsBuilder.build(true).toUri();
ResponseEntity<String> rsStr = restTemplate.exchange(uri, HttpMethod.GET, httpEntity, String.class);
返回值
调用--token验证,返回结果=rsStr:<200,{"resultCode":"200","resultMsg":"正常"}>
参考如下:
Spring RestTemplate发送请求时 自动对参数进行urlencode的问题_wangooo的博客-CSDN博客
|