Uri encode and URL encode handle different spaces

Source: Internet
Author: User

When there are more and more open platforms and openapis, client software is more or less interacting with servers through HTTP and HTTPS protocols. One of the common cases is to encode the URL to ensure the correct transmission of Chinese and special characters. However, this small encode is not as simple as you think.

When I encountered an example in Android development in the past few days, the space character (ASCII code is 0x20) in the URL is passed through java.net. after the urlencoder class encode, it will become the + number, instead of the % 20, which causes the server to be incorrectly identified.

After investigation, urlencode has two methods:

Public static string encode (string S, string charsetname)
Encodes s using the charset named bycharsetname.

 

Public static string encode (string S)

Equivalent to encode (S, "UTF-8 ").

 

The second method has been abandoned since Android API 1, it is recommended to use the first method to display the specified character set as UTF-8, And the encoding is to encode the space character to +, instead of % 20. Of course, this is URL compliant, see the RFC-1738.

 

To encode a space character as % 20, use another encode, The android.net. Uri class, which uses the RFC-2396 standard. Syntax:

Public static string encode (string S)

Encodes characters in the given string as '%'-escaped octets using the UTF-8 scheme. leaves letters ("A-Z", "a-z"), numbers ("0-9"), and unreserved characters ("_-!. ~ '() * ") Intact. encodes all other characters.

 

When implementing network-related interfaces, you must carefully understand the specific differences. We also recommend that you describe protocol agreements as clearly as possible in the documentation of the open platform to avoid difficulties in development and debugging. Imagine how many tests are required to discover this bug if it does not happen to have been discovered?

For the difference between URL and Uri, see Baidu encyclopedia.

-- Welcome to reprint, please indicate the source of http://blog.csdn.net/caowenbin --

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.