Java implements URL transcoding, decoding

Source: Internet
Author: User
Tags rfc

URL Origin:

In general, URLs can use only English letters, Arabic numerals, and certain punctuation marks, and cannot use other text or symbols. For example, the world has the English alphabet url "http://www.abc.com", but there is no Greek alphabet url "http://www.aβγ.com" (read as alpha-beta-gamma. com). This is because the network standard RFC 1738 makes the hard rules:

"... Only alphanumerics [0-9a-za-z], the special characters "$-_.+!*" (), "[not including the quotes-ed], and reserved charact ERs used for their reserved purposes could be used unencoded within a URL. "

"Only letters and numbers [0-9a-za-z], some special symbols" $-_.+!* "()," [not including double quotes], and some reserved words can be used without encoding directly for the URL. " ”

This means that if there is a Chinese character in the URL, it must be encoded and used. However, the trouble is that RFC 1738 does not prescribe specific coding methods, but instead gives the application (browser) its own decision. This causes "URL coding" to become a confusing area.

Let's see how confusing "url coding" is. I'll analyze four different situations in turn, in each case, the URL encoding method of the browser is not the same. After explaining their differences, I'll say how to use JavaScript to find a unified coding method.

The specific code is as follows:(remember, be sure to note the coding)

 PackageCom.util;Importjava.io.UnsupportedEncodingException;/*** URL transcoding, decoding * *@authorLifq * @date 2015-3-17 pm 04:09:35*/ Public classUrlutil {Private Final StaticString ENCODE = "GBK"; /*** URL Decoding * *@returnString *@authorLifq * @date 2015-3-17 pm 04:09:51*/     Public Staticstring geturldecoderstring (String str) {string result= ""; if(NULL==str) {            return""; }        Try{result=Java.net.URLDecoder.decode (str, ENCODE); } Catch(unsupportedencodingexception e) {e.printstacktrace (); }        returnresult; }    /*** URL transcoding * *@returnString *@authorLifq * @date 2015-3-17 pm 04:10:28*/     Public Staticstring geturlencoderstring (String str) {string result= ""; if(NULL==str) {            return""; }        Try{result=Java.net.URLEncoder.encode (str, encode); } Catch(unsupportedencodingexception e) {e.printstacktrace (); }        returnresult; }    /**     *      * @returnvoid *@authorLifq * @date 2015-3-17 pm 04:09:16*/     Public Static voidMain (string[] args) {String str= "Test 1";        SYSTEM.OUT.PRINTLN (geturlencoderstring (str));            SYSTEM.OUT.PRINTLN (geturldecoderstring (str)); }}

Java implements URL transcoding, decoding

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.