URL special character escaping and solving method

Source: Internet
Author: User

    1. URL special characters to be escaped
    2. 1. Replace the space with a plus sign (+)
    3. 2. Forward slash (/) separates directories and subdirectories
    4. 3. Question mark (?) Separating URLs and queries
    5. 4, percent semicolon (%) make special characters
    6. 5. #号指定书签
    7. 6, & Separation parameters


Reasons for escaping characters:

If your form is submitted using the Get method, and the arguments in the submission have a special character such as "&", if you do not do this, the service side will treat the & as a different parameter. For example
The action for the form is list.jsf?act=go&state=5
The value of Act and state can be obtained separately by Request.getparameter when committing.
If your intention is to act= ' go&state=5 ' This string, then to get the exact value of the act on the server, you must escape &

URL Escape Character principle:

These special characters are converted to ASCII code in the form of: the ASCII code of the% plus character, which is a percent percent, followed by the ASCII (16 binary) code value of the corresponding character. For example, the encoded value of a space is "%20".

    1. URL special symbol and corresponding hexadecimal value encoding:
    2. 1. + URL + sign for space%2b
    3. 2. Spaces in the space URL can be used with the + number or code%20
    4. 3./separate directories and subdirectories%2f
    5. 4.? Separating the actual URLs and parameters%3f
    6. 5.% Specify special characters%25
    7. 6. # means bookmark%23
    8. 7. The delimiter between the parameters specified in the & URL%26
    9. 8. = value of the specified parameter in the URL%3d

The workaround is as follows (with the + number as an example):

Method One, modify the client, the client with "+" in the parameters of "+" all replaced with "2b%", so that the parameters to the server side can get "+".

Method Two, modify the server side, the space is replaced with "+", this method only applies to the parameter has "+" in the case of no spaces.

Example:

    1. String a = reuqest. GetParameter ("Clientstr"). Replace (', ' + ');

If the client is Clientstr=test+ok, then the value of a is test+ok;

Method Three, modify the server side, will get the method of the parameter by Reuqest. GetParameter to Request.getquerystring (). substring (0) and parse the resulting 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 and needs to be resolved again.

A=a. substring (10); Gets the value of a to Test+ok.

Attached: A JS that is used to escape special characters in the URL.

    1. function UrlEncode (SSTR)
    2. {
    3. Return Escape (SSTR). Replace (/\+/g, '%2b '). Replace (/\ "/g, '%22 '). Replace (/\ '/g, '%27 '). Replace (/\//g, '%2f ');
    4. }

URL special character escaping and solving method

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: info-contact@alibabacloud.com 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.