原文

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 代替空格。

比如下面这张表包含了部分特殊字符的转化规则。

CharacterFrom Windows-1252From UTF-8
space%20%20
!%21%21
"%22%22
#%23%23
$%24%24
%%25%25
&%26%26
'%27%27
(%28%28
)%29%29

阿东
201 声望54 粉丝