java.lang.IllegalArgumentException: Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986

An error is reported when the WeChat applet uses the get method to transmit parameters in the front and backend as shown in the figure. But it is fine on the WeChat development platform and Apple test, and this error is reported on the Android phone. I guess the reason is that get passed Chinese characters.

 I tried to output the parameters obtained from the foreground in the background, but nothing was obtained in the background, and an error was reported directly.

Finally, the Chinese in the url was replaced with id; because the search function could not replace Chinese, it was changed to a post request.

Reference:
[Java EE] The encoding process of get and post requests
The solution to the garbled Chinese parameters of get requests

 

There are also many reasons for this error on the Internet because it contains special characters:
some versions of Tomcat strictly follow the RFC 3986 specification for access parsing,
and the RFC 3986 specification defines that only English letters (a-zA-Z) and numbers (0) are allowed in Url. -9), -_.~4 special characters and all reserved characters
(RFC3986 specifies the following characters as reserved characters: ! * ' ( ) ; : @ & = + $ , / ? # [ ]).
This error will be reported if the incoming parameter (for example: "{") contains reserved fields that are not in RFC3986. Example: http://localhost:8080/index.do?{id:123}
Solution:
1. Remove special characters in the
url 2. URL encode the parameters
3. Use post to submit
4. Replace the lower version of Tomcat
5 . Add a line at the end of conf/catalina.properties:
org.apache.tomcat.util.buf.UDecoder.ALLOW_ENCODED_SLASH=true
Online also said to add the following sentence to allow forbidden characters: |{}
tomcat.util.http.parser .HttpParser.requestTargetAllow=|{}
Reference: https://blog.csdn.net/testcs_dn/article/details/71716829

Regarding url encoding the parameters:

Example: Encode || in url
(1) The foreground encodes the url:

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

(2) Only encoding parameters:

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

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325354697&siteId=291194637