Java.net.URLEncoder resolve URL Chinese parameter garbled problem "2014-11-17"

Source: Internet
Author: User
Tags alphanumeric characters

1. Coding Rulesjava.netClass Urlencoder
    • Java.lang.Object
      • Java.net.URLEncoder

  • Urlencoderextends Object
    Utility class for HTML form encoding. This class contains static methods for converting a String to the application/x-www-form-urlencoded MIME format. For more information about HTML form encoding, consult the htmlspecification.

    When encoding a String, the following rules apply:

    • The alphanumeric characters " a through" z "," " A through" Z "and" 0 "through" "remain the same 9 .
    • The special characters "", "" "," " . - * , and" _ "remain the same.
    • The space character "is   converted to a plus sign" + .
    • All other characters is unsafe and is first converted to one or more bytes using some encoding scheme. Then each byte was represented by the 3-character string " %xy ", wherexy is the two-digit hexadecimal representat Ion of the byte. The recommended encoding scheme to be UTF-8. However, for compatibility reasons, if a encoding is not specified and then the default encoding of the platform is used.

    For example using UTF-8 as the encoding scheme the string "The Stringü@foo-bar" would get converted to "THE+STRING+%C3%BC %40foo-bar "because in UTF-8 the Characterüis encoded as both bytes C3 (hex) and BC (hex), and the character @ is encoded As one byte (hex).

    Since:
    JDK1.0
    • Method Summary
      Methods
      Modifier and Type Method and Description
      static String encode(String s) Deprecated. The resulting string may vary depending on the platform ' s default encoding. Instead, use the Encode (string,string) method to specify the encoding.
      static String encode(String s,String enc)Translates a string into application/x-www-form-urlencoded format using a specific encoding scheme.
        • Methods inherited from class Java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
      // explain:This is the official note inside the jdk1.7. Roughly, UrlEncode is converting a string to the application/x-www-form-urlencoded MIME format.
    • The rules are as follows: 1, letters and numbers remain as is;
    • 2, ".", "-", "*" and "_" are kept as is, and the space is converted to "+";
    • 3, other characters converted to one or more byte code, such as%ab format;

    Example: The Stringü@foo-bar encoded with Utf-8 as The+string+%c3%bc%40foo-bar

2. Encoding method

There are two methods: Encode (string s) and encode (string s,string encode)

Encode (String s) is encoded using the default encoding of the platform and is used with caution!

Encode (String s,string ENC) is encoded with the specified encoding, preferably in this way, to specify the encoding format, in order to avoid the changes in the environment to produce Chinese garbled



Java.net.URLEncoder resolve URL Chinese parameter garbled problem "2014-11-17"

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.