Stories

Detail Return Return

【Java】What is does %20 and %30 mean in URL - Stories Detail

原文

https://www.quora.com/What-is-does-20-and-30-mean-in-URL

引言

The %20 and %30 in a URL you are referring to is what’s known as URL encoding converts. URL encoding converts characters into a format that can be transmitted over the Internet.

URL當中的%20%30 是用來識別其編碼轉化格式的,URL 編碼將字符轉換成可以在互聯網上傳輸的格式。

比如很多博客網站出來的連接會有類似下面的格式:

[https://blog.csdn.net/weixin_36242811/article/details/8921614...中類獲取同級文件&spm=1000.2123.3001.4430]

其中 %20 就是被轉化過的編碼格式。

有意思的是,如果我們把 %20 放到IDEA 全局搜索,會發現有很多編程語言會處理這種”特殊情況“:

比如jquery.js 會做下面的處理:

// Change '%20' to '+' if this is encoded form body content (gh-2658)  
} else if ( s.data && s.processData &&  
   ( s.contentType || "" ).indexOf( "application/x-www-form-urlencoded" ) === 0 ) {  
   s.data = s.data.replace( r20, "+" );  
}

在比如hutool中的方法對於空格進行特殊編碼:

/**
 * 單獨編碼URL中的空白符,空白符編碼為%20
 *
 * @param urlStr URL字符串
 * @return 編碼後的字符串
 * @since 4.5.14
 */
public static String encodeBlank(CharSequence urlStr) {
    if (urlStr == null) {
        return null;
    }

    int len = urlStr.length();
    final StringBuilder sb = new StringBuilder(len);
    char c;
    for (int i = 0; i < len; i++) {
        c = urlStr.charAt(i);
        if (CharUtil.isBlankChar(c)) {
            sb.append("%20");
        } else {
            sb.append(c);
        }
    }
    return sb.toString();
}

有了這些鋪墊,下面介紹的內容,基本上看一眼英文就知道在介紹什麼了。

URL Encoding (Percent Encoding)

URLs can only be sent over the Internet using the ASCII character-set.

URL 編碼將字符轉換成可在互聯網上傳輸的格式。

Since URLs often contain characters outside the ASCII set, the URL has to be converted into a valid ASCII format.

由於 URL 通常包含 ASCII 字符集以外的字符,因此必須將 URL 轉換為有效的 ASCII 格式。

URL encoding replaces unsafe ASCII characters with a "%" followed by two hexadecimal digits.

URL 編碼將不安全的 ASCII 字符替換為"%",然後是兩個十六進制數字。

URLs cannot contain spaces. URL encoding normally replaces a space with a plus (+) sign or with %20.

URL 不能包含空格。URL 編碼通常用加號 (+) 或 %20 代替空格。

ASCII Encoding Reference

%20 代表字符“空格”

Character - space

From Windows-1252 - %20

From UTF-8 - %20

%30 代表字符“0”

Character - 0

From Windows-1252 - %30

From UTF-8 - %30

ASCII Encoding Reference

See the full ASCII Encoding Reference here - HTML URL Encoding Reference

我們可以看下看下W3C網站,有一張對於字符串編碼代碼格式對應表:

URL encoding converts characters into a format that can be transmitted over the Internet.
URL 編碼將字符轉換成可在互聯網上傳輸的格式。

URLs can only be sent over the Internet using the ASCII character-set.
URL 只能通過 ASCII 字符集互聯網上傳輸。

Since URLs often contain characters outside the ASCII set, the URL has to be converted into a valid ASCII format.
由於 URL 通常包含 ASCII 字符集以外的字符,因此必須將 URL 轉換為有效的 ASCII 格式

URL encoding replaces unsafe ASCII characters with a "%" followed by two hexadecimal digits.
URLs cannot contain spaces. URL encoding normally replaces a space with a plus (+) sign or with %20.

URL 編碼將不安全的 ASCII 字符替換為"%",然後是兩個十六進制數字。
URL 不能包含空格。URL 編碼通常用加號 (+) 或 %20 代替空格。

比如下面這張表包含了部分特殊字符的轉化規則。

Character From Windows-1252 From UTF-8
space %20 %20
! %21 %21
" %22 %22
# %23 %23
$ %24 %24
% %25 %25
& %26 %26
' %27 %27
( %28 %28
) %29 %29
user avatar mannayang Avatar chenjiabing666 Avatar tuhooo Avatar changqingdezi Avatar enaium Avatar sorra Avatar jeremylai7 Avatar guaiguaidedoujiang_cmg5bc Avatar object_684147fd5fae2 Avatar cbuc Avatar
Favorites 10 users favorite the story!
Favorites

Add a new Comments

Some HTML is okay.