The connection and difference between the Urlencoder and Urldecoder classes in Java

Source: Internet
Author: User
Tags alphanumeric characters

Today, we find some information to summarize: the connection and difference between Urlencoder and Urldecoder classes in Java.

First of all, the connection and difference between the two:

Urlencoder is encoded, Urldecoder is decoded. The conversion process is just the opposite. Urlencoder The class contains a static method that converts a string to application/x-www-form-urlencoded MIME format; Urldecoder The class contains a application/x-www-form-urlencoded static method that decodes a string from MIME format. Mainly to solve the problem of garbled, garbled the emergence of a great possibility is the coding and decoding problems.

java.netClass Urlencoder
Java.lang.Object Java.net.URLEncoder

A utility class that is encoded in HTML format. The class contains a static method that converts a String to application/x-www-form-urlencoded MIME format.

When encoding String, use the following rules:


    • The alphanumeric characters " a to" z , "to" A and "to" Z remain unchanged 0 9 .

    • Special characters "," "," " . - and" * _ "remain unchanged.

    • The space character " " is converted to a plus sign " + ".

    • All other characters are unsafe, so you first use some encoding mechanisms to convert them to one or more bytes. Each byte is then represented by a 3-character string " %xy ", where xy is the two-bit hexadecimal representation of the byte. The recommended encoding mechanism is UTF-8. However, for compatibility reasons, if an encoding is not specified, the default encoding for the corresponding platform is used.

For example, using the UTF-8 encoding mechanism, the string "The Stringü@foo-bar" will be converted to "The+string+%c3%bc%40foo-bar" because in UTF-8, the character U is encoded as two bytes, C3 (hex) and BC (16 in Character @ is encoded as a byte 40 (hexadecimal).

    • Start from the following versions:

    • JDK1.0

The Encode method in Urlencoder class--coding
public static string encode (string s, String enc) throws Unsupportedencodingexception
    • Converts a string to a format using the specified encoding mechanism application/x-www-form-urlencoded . The method uses the provided encoding mechanism to obtain the bytes of the unsafe character.

      Note: The World Wide Web Consortium recommendation declares that UTF-8 should be used. If you do not use this encoding, it may cause incompatibilities.


      • Parameters:

      • s-To convert String .

      • enc-The name of the supported character encoding.

      • Return:

      • The converted String .

      • Thrown:

      • UnsupportedEncodingException-If the specified encoding is not supported

      • Start from the following versions:

      • 1.4

java.netClass Urldecoder
Java.lang.Object Java.net.URLDecoder

A utility class that is decoded in HTML format. This class contains a application/x-www-form-urlencoded static method that decodes a String from MIME format.

The conversion process is exactly the reverse of the process used by the Urlencoder class. Assume that all characters in the encoded string are one of the following: "To", "to", "to" and ",", "", and " a z A Z 0 9 - _ . * . The " % " character is allowed, but it is interpreted as the start of a special escape sequence.

The following rules are used in the conversion:


    • The alphanumeric characters " a to" z , "to" A and "to" Z remain unchanged 0 9 .

    • Special characters "," "," " . - and" * _ "remain unchanged.

    • The plus sign " + " is converted to the space character " ".

    • The " %xy " format sequence is treated as a byte, where xy is a two-bit hexadecimal representation of 8 bits. Then, all substrings that contain one or more of these byte sequences consecutively will be replaced by characters whose encoding can generate these contiguous bytes. You can specify the encoding mechanism for decoding these characters, or, if not specified, the default encoding mechanism of the platform.

There are two possible ways that the decoder can handle illegal strings. One method is to throw IllegalArgumentException an exception regardless of the illegal character. Which method the decoder body uses depends on the implementation.

    • Start from the following versions:

    • 1.2

The Decode method in Urldecoder--decoding
public static string decode (string s, String enc) throws Unsupportedencodin Gexception
    • Decodes a string using the specified encoding mechanism application/x-www-form-urlencoded . The given encoding is used to determine the %xy character that is represented by a sequential sequence of any "" format.

      Note: The World Wide Web Consortium recommendation declares that UTF-8 should be used. If you do not use this encoding, it may cause incompatibilities.


      • Parameters:

      • s-To decode theString

      • enc-The name of the supported character encoding.

      • Return:

      • The newly decodedString

      • Thrown:

      • UnsupportedEncodingException-If a reference character encoding is required and the specified character encoding is not supported

      • Start from the following versions:

      • 1.4

The above is the study notes, the shortcomings please the Great God guidance.

This article is from the "Forget Worry Grass" blog, please be sure to keep this source http://qinglongleijun.blog.51cto.com/9625764/1582112

Connections and differences between Urlencoder and Urldecoder classes in Java

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.