之前有个需求调用对方的接口,但是由于请求URL中带有json数据,导致406
URL:
https://xxx.xx.com/xx/vip/vipSrfCgi?serviceName=xxxxx&requestBody={"appid":"90089","orderid":"hth123456789","uin":"1635618490","conn_appid":"","time":"1615367453","token":"771e74bfc8a3940b094cecdf37957b80"}&__debugid=1158542577
请求报错:
查了挺多地方都没有找到,直到有一次在服务器curl才看出猫腻,对json字符串做了处理
处理后:
curl -H 'Content-Type:application/json' -x 10.107.100.64:9090 -X GET "https://xxx.xx.com/xx/vip/vipSrfCgi?serviceName=xxxxx&requestBody=\\{\"appid\":\"108017\",\"orderid\":\"hth123456789\",\"uin\":\"1635618490\",\"time\":\"1615452087\",\"token\":\"a6950569a76f8dc2398451fd2088e825\"\\}&__debugid=1158542577"
(这里我是转义过的,但是贴出来后总是不对,这里要对双引号进行转义)
因为我是用restTemplate发请求的,我们主要对URL进行处理下:
public String getForQQString(String url, String request, Class<String> responseType) {
try {
URI uri = UriComponentsBuilder.fromUriString(url).build().encode().toUri();
HttpEntity<String> entity = new HttpEntity<>(request, headers);
ResponseEntity<String> exchange = restTemplate.exchange(uri, HttpMethod.GET, entity, String.class);
return exchange.getBody();
} catch (HttpClientErrorException e) {
return e.getResponseBodyAsString();
}
}