1. 程式人生 > >java.lang.IllegalArgumentException: Invalid character found in the request target. The valid charact

java.lang.IllegalArgumentException: Invalid character found in the request target. The valid charact

技術分享圖片

 嘗試了下在後臺輸出從前臺獲取的引數,但是後臺什麼也沒有獲取到,直接報錯。

最後用id替換了url裡的中文;因為搜尋功能不能替換中文的,改成了post請求。

網上也有很多報這個錯的原因是含有特殊字元: 有些版本的Tomcat嚴格按照 RFC 3986規範進行訪問解析, 而 RFC 3986規範定義了Url中只允許包含英文字母(a-zA-Z)、數字(0-9)、-_.~4個特殊字元以及所有保留字元 (RFC3986中指定了以下字元為保留字元:! * ’ ( ) ; : @ & = + $ , / ? # [ ])。 傳入的引數(例:"{")中有不在RFC3986中的保留欄位中,會報這個錯。例:http://localhost:8080/index.do?{id:123} 解決: 1. 去掉url中的特殊字元 2. 對引數進行url編碼 3. 使用post提交 4. 更換更低版本的Tomcat 5. 在 conf/catalina.properties 中最後新增一行: org.apache.tomcat.util.buf.UDecoder.ALLOW_ENCODED_SLASH=true 網上也有說新增下面一句,來允許禁止的字元:|{} tomcat.util.http.parser.HttpParser.requestTargetAllow=|{}  參考:

https://blog.csdn.net/testcs_dn/article/details/71716829

關於對引數進行url編碼:

例:對url中的||進行編碼 (1) 前臺對url編碼:

encodeURI("http://localhost:8080/app/handleResponse?msg=name|id|")
> http://localhost:8080/app/handleResponse?msg=name%7Cid%7C

(2)只編碼引數:

encodeURIComponent("msg=name|id|")
> msg%3Dname%7Cid%7C