When a character from the reserved set (a "reserved character") has
special meaning (a "reserved purpose") in a certain context, and a URI
scheme says that it is necessary to use that character for some other
purpose, then the character must be percent-encoded. Percent-encoding
a reserved character involves converting the character to its
corresponding byte value in ASCII and then representing that value as
a pair of hexadecimal digits. The digits, preceded by a percent sign
("%") which is used as an escape character, are then used in the URI
in place of the reserved character. (For a non-ASCII character, it is
typically converted to its byte sequence in UTF-8, and then each byte
value is represented as above.) The reserved character "/", for
example, if used in the "path" component of a URI, has the special
meaning of being a delimiter between path segments. If, according to a
given URI scheme, "/" needs to be in a path segment, then the three
characters "%2F" or "%2f" must be used in the segment instead of a raw
"/".
这不仅适用于url中的与号,而且适用于所有reserved characters。其中包括:
其思想与在HTML文档中对
&
进行编码相同,但除了在HTML文档中之外,上下文也已更改为在URI中。因此,百分比编码防止了在两个上下文中解析的问题。当你需要将一个URL放在另一个URL中时,这个方法就非常有用了。例如,如果要在Twitter上发布状态:
我的Tweet中有很多保留字符,即
?'():/
,因此我对status
URL参数的整个值进行了编码。这在使用具有消息正文或主题的mailto:
链接时也很有用,因为您需要对body
和subject
参数进行编码,以保持换行符、与号等的完整性。http://en.wikipedia.org/wiki/Percent-encoding#Percent-encoding_reserved_characters
它们需要百分比编码:
所以在你的例子中,URL看起来像:
尝试使用
http://www.example.org?candy_name=M%26M
。另请参见this reference和一些详细信息on Wikipedia。
相关问题 更多 >
编程相关推荐