Problems with string transcoding to URL

Source: Internet
Author: User

Parameter processing, url = "http: // 8080/androidserver/music/Eason Chan-exaggerated"
           In Chan yixun-exaggerated Before adding a URL, encode it first.
           Example: String Mm = "Chan yixun-float ";        
                    String Ss = urlencoder. encode (Mm, "UTF-8 "); // Here is the string encoded into a UTF-8
                     System. Out. println ("SS =" + SS );   
        // Output result  Ss=chen Yi xunqiang -w.e6}�
        // The space is encoded+ In this way, there is still a problem with the Space Encoding after the encoding, which needs to be processed. Continue below
                     Ss = ss. replaceall ("\\+ ","");// Because +Symbols are key characters in Java and must be escaped.
//It is the space encoding. If it is replaced here, no error will be reported for the URL.
                     Url = "http: /// 8080/androidserver/music/" + SS
                     Such URL textThe space parameter problem is solved. 






........................................................................................................................ ..................

URL special character escape and Solution
  1. Special characters in the URL must be escaped.
  3. 1. Replace space with the plus sign (+)
  4. 2. The forward slash (/) is used to separate directories and subdirectories.
  5. 3. Question mark (?) Separate URLs and queries
  6. 4. Specify special characters for the percent sign (% ).
  7. 5. # specifying bookmarks
  8. 6. & # separator Parameters 

Causes of escape characters:

If your form is submitted using the get method and the submitted parameters contain special characters such, on the service end, the & parameter is treated as another parameter. For example
The form action is list. JSF? Act = Go & State = 5
Then, request. getparameter can be used to obtain the values of act and state.
If your intention is the act = 'go & State = 5' string, You must escape & to get the exact act value on the server.

Principles of URL escape characters:

Convert these special characters into ASCII codes in the format of % plus the ASCII code, that is, a percent sign %, followed by the ASCII (hexadecimal) value of the corresponding characters. For example, the Space Encoding value is "".

  1. The URL special symbol and the corresponding hexadecimal value encoding:
  3. 1.+ URL"+" Indicates space.+
  4. 2.SpaceSpaces in the URL can be encoded with a plus sign (+ ).
  5. 3./ Separate directories and subdirectories/ 
  6. 4.? Separate the actualURLAnd Parameters? 
  7. 5.%Special characters% 
  8. 6.#Indicates bookmarks# 
  9. 7.&URLThe delimiter between the specified parameters in& 
  10. 8.=URLParameter Value in= 

Solution ):

Method 1,Modify the client and replace "+" in the "+" parameter of the client with "+" with "2B %". Then, when the parameter is uploaded to the server, "+" is displayed.

Method 2,Modify the server side and replace the space with "+". This method is only applicable when the parameter contains "+" and no space.


  1. StringA=Reuqest. getparameter ("clientstr"). Replace ('',' + ');

If the client is clientstr = test + OK, 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 parse the obtained string.


  1. StringA=Request. getquerystring (). substring (0 );

If the client is clientstr = test + OK, then the value of A is clientstr = test + OK. You need to parse it again,

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


Appendix:A JavaScript code used to escape special characters in a URL.


  1. FunctionUrlencode (SSTR)
  2. {
  3.     ReturnEscape (SSTR). Replace (/\ +/g,'+'). Replace (/\ "/g, '"'). Replace (/\ '/g,'''). Replace (///g ,'/');
  4. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.