Space in URL Encoding

Source: Internet
Author: User

When investigating an error, I accidentally found that some links on the HTML page contain +, decompiled the tag, and found that the java.net. urlencoder method was called.

Public static string encode (string S, string ENC)

From the code, we can clearly see that you have taken special care of converting ''into '+ '. Because the following URL encoding rules should convert spaces to % 20, Google gave it a try. It turns out that many people have encountered this problem, but it is basically not detailed.

It took more than an hour to search carefully. The clues are as follows:

1. Someone found in Sun's bug library that this method does not comply with the rfc2396 standard.
Http://bugs.sun.com/view_bug.do? Bug_id = 4616184
Sun replied that it is not a bug and meets the html4.01 standard.

2. Find instructions on HTML standards in W3C
Http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.4
Here we can clearly see that the encoding method is different based on the contexttype, when the contexttype of form is [X-WWW-form-urlencoded], the key/value pairs in form are encoded, and spaces are converted to +. Other characters follow[Rfc1738]The standard format is % HH.

3. Check urlencoder again.
The annotations of this class are clearly written:
Converting a string to the application/X-WWW-form-urlencoded MIME format

From the above results, % 20 seems to be a misunderstanding in use. Because % 20 can be parsed into spaces, it is assumed that spaces should be escaped as % 20.

Further, for Java /. net/JavaScript-related functions are tested. The results show that Java (1.5) and.. Net (2.0), but the JavaScript function still converts spaces to % 20. It seems that the misunderstanding of this problem is entirely due to the JS error ......

 

Public static void main (string [] ARGs )...{
Try ...{
System. Out. println (urlencoder. encode ("Hello World", "UTF-8 "));
System. Out. println (urldecoder. Decode ("Hello + world", "UTF-8 "));
System. Out. println (urldecoder. Decode ("Hello % 20 World", "UTF-8 "));
} Catch (unsupportedencodingexception e )...{
E. printstacktrace ();
}
}

 

Static void main ()
...{
Console. writeline (httputility. urlencode ("Hello World "));
Console. writeline (httputility. urldecode ("Hello World "));
Console. writeline (httputility. urldecode ("Hello % 20 World "));
}

<Script language = "JavaScript" type = "text/JavaScript">
Document. Write (escape ('Hello World') + '<br> ');
Document. Write (Unescape ('Hello + World') + '<br> ');
Document. Write (Unescape ('Hello % 20world '));
</SCRIPT>

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.