About the URL in Java to pass the Chinese parameter, the value is garbled solution

Source: Internet
Author: User

A few days ago to see a netizen in Ask Urldecoder and urlencoder aspects of the use of the problem, suddenly remembered, I just met these two classes, also feel very close, so you can think of a beginner's mood, so I have this article today.
In fact, the use of these two classes is not complex, urldecoder and urlencoder its role is mainly for ordinary strings and application/x-www-form-rulencodedmime between the string conversion, The average person will think that after a string comparison professional, think there is any advanced knowledge, not actually.

Below, we search for "Network time and Space" in "Baidu", we will see the diagram shown below


It can be seen from this: when we search the key word contains Chinese, these key words will become the "garbled", in fact, this is not garbled, this is called Applicaion/x-www-form-urlencodedmime string.
When a string containing non-Western European characters is included in the URL address, the system will convert these non-Western European special strings, then the encoding process may involve the normal string and this special string related conversion, which is required to use the Urldecoder and Urlencoder class

The Urldecoder class contains a decode (string s,stringcharcter) static method that converts a special string that looks garbled to a normal string
The Urlencoder class contains a encode (string s,stringcharcter) static method that converts a normal string to a application/x-www-form-urlencodedmime string

The following program code
importjava.net.*;

public class Urldecodertest
{
public static void Main (string[] args)
Throws Exception
{
The application/x-www-form-urlencoded string
Convert to normal string
Where the string is copied directly from the window shown.
String KeyWord =urldecoder.decode (
"%cd%f8%c2%e7%ca%b1%bf%d5", "UTF-8");
System.out.println (KeyWord);
Converts a normal string into a
application/x-www-form-urlencoded string
String Urlstr =urlencoder.encode (
"Network Time and Space", "UTF-8");
System.out.println (URLSTR);
}
}


The above program is the conversion between the normal Chinese string and the application/x-www-form-urlencoded mime string.

Run the above program result diagram as follows

About the URL in Java to pass the Chinese parameter, the value is garbled solution

Related Article

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.