解決辦法:將這些字符轉(zhuǎn)化成服務(wù)器可以識(shí)別的字符晒奕,對(duì)應(yīng)關(guān)系如下:
URL字符轉(zhuǎn)義
+ URL 中+號(hào)表示空格 %2B
空格 URL中的空格可以用+號(hào)或者編碼 %20
/ 分隔目錄和子目錄 %2F
? 分隔實(shí)際的URL和參數(shù) %3F
% 指定特殊字符 %25
# 表示書簽 %23
& URL 中指定的參數(shù)間的分隔符 %26
= URL 中指定參數(shù)的值 %3D
@Test
public void test1() throws UnsupportedEncodingException {
HttpHeaders headers = new HttpHeaders();
headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);
String url = "http://localhost:8000/?";
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url)
.queryParam("Version", "2016-03-04")
.queryParam("Search", "S中文Ktest123#@!+ /?%&=123漢字");
HttpEntity<?> entity = new HttpEntity<>(headers);
restTemplate.exchange(builder.build().encode().toUri(), HttpMethod.GET, entity, JSONObject.class);
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
restTemplate.exchange(builder.build().encode().toUri(), HttpMethod.POST, entity, JSONObject.class);
String encode = URLEncoder.encode("S中文Ktest123#@!+ /?%&=123漢字", "UTF-8");
String param = "Search=" + encode;
HttpEntity<?> entity2 = new HttpEntity<>(param, headers);
restTemplate.exchange(url, HttpMethod.POST, entity2, JSONObject.class);
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者