URL special character escape and solution

  1. URL special characters need to be escaped   
  2.  
  3. 1. Replace spaces with plus signs (+)   
  4. 2. Forward slash (/) separates directories and subdirectories   
  5. 3. Question mark (?) separates URL and query   
  6. 4. The percent sign (%) defines special characters   
  7. 5. # Specify bookmarks   
  8. 6, ampersand separated parameters  


The reason for the escape character: 

If your form is submitted using the get method, and there are special characters such as "&" in the submitted parameter, if it is not processed, the service side will treat the following & as another parameter. For example 
, if the action of the form is list.jsf?act= Go &state=5 
, the values ​​of act and state can be obtained respectively through request.getParameter when submitting. 
If your intention is the string act='go&state=5', then in order to get the exact value of act on the server, you must escape & 

url escape character principle: 

convert these special characters into ASCII codes , the format is: % plus the ASCII code of the character, that is, a percent sign %, followed by the ASCII (hexadecimal) code value of the corresponding character. For example the encoded value of space is "%20". 
 

  1. URL special symbols and corresponding hexadecimal value encoding:   
  2.  
  3. 1. + The + sign in the URL represents a space %2B   
  4. 2. Spaces Spaces in URLs can be encoded with a + sign or %20   
  5. 3. / separates directories and subdirectories %2F    
  6. 4. ? separates the actual URL from the parameter %3F    
  7. 5. % specifies the special character %25    
  8. 6. # means bookmark %23    
  9. 7. & separator between parameters specified in URL %26    
  10. 8.  =   The value of the specified parameter in the   URL %3D

The solution is as follows (take the + sign as an example):

Method 1. Modify the client, and replace all the "+" in the parameters with "+" on the client with ‍"2B%", so that the "+" can be obtained when the parameter is passed to the server.

Method 2: Modify the server side and replace the space with "+". This method is only applicable to the case where there is ‍"+" in the parameter but no space.

example:

  1. String a = reuqest.‍getParameter("clientStr")‍.replace(' ','+'); 

If the client is clientStr=test+OK, then the value of a is test+OK;

 

Method 3: Modify the server side, change the method for obtaining parameters from ‍reuqest.‍getParameter to ‍request.getQueryString().substring(0), and then parse the obtained string.

example:

  1. ‍‍String a =request.getQueryString().substring(0); 

‍If the client is clientStr=test+OK, then the value of a is ‍clientStr=test+OK, which needs to be parsed again.

a=a.‍substring(10); The value of a is ‍test+OK.

 

Attachment: A JS for escaping special characters in URLs.

 

  1. ‍function URLencode(sStr)   
  2. {  
  3.     return escape(sStr).replace(/\+/g, '%2B').replace(/\"/g,'%22').replace(/\'/g, '%27').replace(/\//g,'%2F');  
  4. }

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326990663&siteId=291194637