url参数中有+、空格、=、%、&、#等特殊符号的问题解决

个人专题目录


解决办法:将这些字符转化成服务器可以识别的字符,对应关系如下:
URL字符转义
+    URL 中+号表示空格                      %2B
空格  URL中的空格可以用+号或者编码           %20
/    分隔目录和子目录                       %2F
?    分隔实际的URL和参数                    %3F
%    指定特殊字符                          %25       
#    表示书签                              %23
&    URL 中指定的参数间的分隔符             %26
=    URL 中指定参数的值                    %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);
}

你可能感兴趣的:(url参数中有+、空格、=、%、&、#等特殊符号的问题解决)