url參數(shù)中有+、空格、=、%、&、#等特殊符號的問題解決

個人專題目錄


解決辦法:將這些字符轉(zhuǎn)化成服務器可以識別的字符,對應關系如下:
URL字符轉(zhuǎn)義
+    URL 中+號表示空格                      %2B
空格  URL中的空格可以用+號或者編碼           %20
/    分隔目錄和子目錄                       %2F
?    分隔實際的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);
}
最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容