fbpx
维基百科

百分号编码

百分号编码(英語:Percent-encoding),又稱:URL编码URL encoding)是特定上下文的统一资源定位符 (URL)的编码机制,实际上也适用于统一资源标志符(URI)的编码。也用于为 application/x-www-form-urlencoded MIME准备数据,因为它用于通过HTTP的请求操作(request)提交HTML表单数据。

URI的百分号编码 编辑

URI的字符类型 编辑

URI所允许的字符分作保留未保留保留字符是那些具有特殊含义的字符,例如:斜线字符用于URL(或URI)不同部分的分界符;未保留字符没有这些特殊含义。百分号编码把保留字符表示为特殊字符序列。上述情形随URI与URI的不同版本规格会有轻微的变化。

RFC 3986 section 2.2 保留字符 (2005年1月)
! * ' ( ) ; : @ & = + $ , / ? # [ ]
RFC 3986 section 2.3 未保留字符 (2005年1月)
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
a b c d e f g h i j k l m n o p q r s t u v w x y z
0 1 2 3 4 5 6 7 8 9 - _ . ~

URI中的其它字符必须用百分号编码。

对保留字符的百分号编码 编辑

如果一个保留字符在特定上下文中具有特殊含义(称作"reserved purpose") , 且URI中必须使用该字符用于其它目的, 那么该字符必须百分号编码。百分号编码一个保留字符,首先需要把该字符的ASCII的值表示为两个16进制的数字,然后在其前面放置转义字符("%"),置入URI中的相应位置。(对于非ASCII字符, 需要转换为UTF-8字节序, 然后每个字节按照上述方式表示.)

例如,"/", 如果用作URI的路径成份的分界符, 则是具有特殊含义的保留字符. 如果该字符需要出现在URI一个路径成分的内部, 则三字符序列"%2F"或"%2f"就用于代替原本的"/"出现在该URI路径成分的内部.

保留字符的百分号编码
! # $ & ' ( ) * + , / : ; = ? @ [ ]
%21 %23 %24 %26 %27 %28 %29 %2A %2B %2C %2F %3A %3B %3D %3F %40 %5B %5D

在特定上下文中没有特殊含义的保留字符也可以被百分号编码,在语义上与不百分号编码的该字符没有差别.

在URI的"查询"成分(?字符后的部分)中, 例如"/"仍然是保留字符但是没有特殊含义,除非一个特定的URI有其它规定. 该/字符在没有特殊含义时不需要百分号编码.

如果保留字符具有特殊含义,那么该保留字符用百分号编码的URI与该保留字符仅用其自身表示的URI具有不同的语义。

对未保留字符的百分号编码 编辑

未保留字符不需要百分号编码.

两个URI的差别如果仅在于未保留字符是用百分号编码还是用字符自身表示,那么这两个URI具有等价的语义. 但URI处理器实际上并不总是把二者视作等价[來源請求]. 例如, URI的消费者不应该把"%41"与"A", "%7E"与"~"视作不同, 但是某些URI的消费者就是这么做了. 为了最大的互操作性, URI的制造者不应该对未保留字符进行百分号编码。

对百分号字符的百分号编码 编辑

由于百分号字符("%")表示百分号编码字节流的存在, 因此百分号字符应该被编码为3个字节的序列:"%25",用于URI内部。

对任意数据的百分号编码 编辑

大多数URI涉及表示任意数据, 例如IP地址文件系统路径作为URI的成分。

二进数据 编辑

1994年发布的RFC 1738规定[1], URI中的二进制数据应该表示为8位元组的序列,然后对每个8位元组按照上述方式百分号编码. 例如,字节值0F (十六进制)应表示为"%0F", 字节值41(十六进制)应表示为"A"或"%41". 优先使用未保留字符来表示这些字节值,因为这使得URL更短.

字符数据 编辑

二进数据的百分号编码过程已经被外推到字符数据,甚至到不适合或未被完全规范的地步. 在WWW初创阶段,仅仅处理ASCII字符是否编码问题,还没有什么问题。但随后发展到对非ASCII字符如何在URI中编码,缺少标准规范的情况下导致了歧义性的解释URI的错误。

例如, 基于RFC 1738与RFC 2396的协议规定,字符数据先要根据某种字符编码转换为字节流,然后再表示为URI。如果URI不提供是何种字符编码的提示信息,那么这个URI难以可靠的解析。

当前标准 编辑

2005年1月发布的RFC 3986,建议所有新的URI必须对未保留字符不加以百分号编码;其它字符建议先转换为UTF-8字节序列, 然后对其字节值使用百分号编码。此前的URI不受此标准的影响。

非标准的实现 编辑

有一些不符合标准的把Unicode字符在URI中表示为: %uxxxx, 其中xxxx是用4个十六进制数字表示的Unicode的码位值。任何RFC都没有这样的字符表示方法,并且已经被W3C拒绝 (页面存档备份,存于互联网档案馆)。第三版的ECMA-262仍然包含函数escape(string)使用这种语法, 但也有函数encodeURI(uri)转换字符到UTF-8字节序列并用百分号编码每个字节。

application/x-www-form-urlencoded类型 编辑

当HTML表单中的数据被提交时,表单的域名与值被编码并通过HTTP的GET或者POST方法甚至更古远的email[2]把请求发送给服务器。这里的编码方法采用了一个非常早期的通用的URI百分号编码方法,并且有很多小的修改如換行规范化以及把空格符的编码"%20"替换为"+" 。按这套方法编码的数据的MIME类型是application/x-www-form-urlencoded,当前仍用于(虽然非常过时了)HTML与XForms规范中。此外,CGI规范包括了web服务器如何解码这类数据、利用这类数据的内容。

如果发送的是HTTP GET请求,application/x-www-form-urlencoded数据包含在所请求URI的查询成分中。如果发送的是HTTP POST请求或通过email,数据被放置在消息体中,媒体类型的名字被包含在消息的Content-Type头内部。

参见 编辑

参考文献 编辑

  1. ^ RFC 1738 §2.2; RFC 2396 §2.4; RFC 3986 §1.2.1, 2.1, 2.5
  2. ^ User-agent support for email based HTML form submission, using a 'mailto' URL as the form action, was proposed in RFC 1867 section 5.6, during the HTML 3.2 era. Various web browsers implemented it by invoking a separate email program or using their own rudimentary SMTP capabilities. Although sometimes unreliable, it was briefly popular as a simple way to transmit form data without involving a web server or CGI scripts.

外部链接 编辑

百分号编码, 英語, percent, encoding, 又稱, url编码, encoding, 是特定上下文的统一资源定位符, 的编码机制, 实际上也适用于统一资源标志符, 的编码, 也用于为, application, form, urlencoded, mime准备数据, 因为它用于通过http的请求操作, request, 提交html表单数据, 目录, uri的, uri的字符类型, 对保留字符的, 对未保留字符的, 对百分号字符的, 对任意数据的, 二进数据, 字符数据, 当前标准, 非标准的实现,. 百分号编码 英語 Percent encoding 又稱 URL编码 URL encoding 是特定上下文的统一资源定位符 URL 的编码机制 实际上也适用于统一资源标志符 URI 的编码 也用于为 application x www form urlencoded MIME准备数据 因为它用于通过HTTP的请求操作 request 提交HTML表单数据 目录 1 URI的百分号编码 1 1 URI的字符类型 1 2 对保留字符的百分号编码 1 3 对未保留字符的百分号编码 1 4 对百分号字符的百分号编码 1 5 对任意数据的百分号编码 1 5 1 二进数据 1 5 2 字符数据 1 6 当前标准 1 7 非标准的实现 2 application x www form urlencoded类型 3 参见 4 参考文献 5 外部链接URI的百分号编码 编辑URI的字符类型 编辑 URI所允许的字符分作保留与未保留 保留字符是那些具有特殊含义的字符 例如 斜线字符用于URL 或URI 不同部分的分界符 未保留字符没有这些特殊含义 百分号编码把保留字符表示为特殊字符序列 上述情形随URI与URI的不同版本规格会有轻微的变化 RFC 3986 section 2 2 保留字符 2005年1月 a href E5 8F B9 E5 8F B7 html title 叹号 a a href E6 98 9F E8 99 9F html title 星號 a a href E6 92 87 E5 8F B7 html title 撇号 a a href Bracket html class mw redirect title Bracket a a href Bracket html class mw redirect title Bracket a a href E5 88 86 E8 99 9F html title 分號 a a href E5 86 92 E5 8F B7 html title 冒号 a a href html title a a href 26 html title amp amp a a href Equal sign html class mw redirect title Equal sign a a href E5 8A A0 E5 8F B7 E4 B8 8E E5 87 8F E5 8F B7 html title 加号与减号 a a href 24 html title a a href E9 80 97 E8 99 9F html title 逗號 a a href E6 96 9C E7 B7 9A html title 斜線 a a href E9 97 AE E5 8F B7 html title 问号 a a href Number sign html class mw redirect title Number sign a a href Bracket html class mw redirect title Bracket a a href Bracket html class mw redirect title Bracket a RFC 3986 section 2 3 未保留字符 2005年1月 a href A html title A A a a href B html title B B a a href C html title C C a a href D html title D D a a href E html title E E a a href F html title F F a a href G html title G G a a href H html title H H a a href I html title I I a a href J html title J J a a href K html title K K a a href L html title L L a a href M html title M M a a href N html title N N a a href O html title O O a a href P html title P P a a href Q html title Q Q a a href R html title R R a a href S html title S S a a href T html title T T a a href U html title U U a a href V html title V V a a href W html title W W a a href X html title X X a a href Y html title Y Y a a href Z html title Z Z a a href A html title A a a a href B html title B b a a href C html title C c a a href D html title D d a a href E html title E e a a href F html title F f a a href G html title G g a a href H html title H h a a href I html title I i a a href J html title J j a a href K html title K k a a href L html title L l a a href M html title M m a a href N html title N n a a href O html title O o a a href P html title P p a a href Q html title Q q a a href R html title R r a a href S html title S s a a href T html title T t a a href U html title U u a a href V html title V v a a href W html title W w a a href X html title X x a a href Y html title Y y a a href Z html title Z z a a href 0 html title 0 0 a a href 1 html title 1 1 a a href 2 html title 2 2 a a href 3 html title 3 3 a a href 4 html title 4 4 a a href 5 html title 5 5 a a href 6 html title 6 6 a a href 7 html title 7 7 a a href 8 html title 8 8 a a href 9 html title 9 9 a a href E8 BF 9E E5 AD 97 E5 8F B7 html title 连字号 a a href Underscore html class mw redirect title Underscore a a href E5 8F A5 E5 8F B7 html title 句号 a a href E6 B3 A2 E6 B5 AA E8 99 9F html title 波浪號 a URI中的其它字符必须用百分号编码 对保留字符的百分号编码 编辑 如果一个保留字符在特定上下文中具有特殊含义 称作 reserved purpose 且URI中必须使用该字符用于其它目的 那么该字符必须百分号编码 百分号编码一个保留字符 首先需要把该字符的ASCII的值表示为两个16进制的数字 然后在其前面放置转义字符 置入URI中的相应位置 对于非ASCII字符 需要转换为UTF 8字节序 然后每个字节按照上述方式表示 例如 如果用作URI的路径成份的分界符 则是具有特殊含义的保留字符 如果该字符需要出现在URI一个路径成分的内部 则三字符序列 2F 或 2f 就用于代替原本的 出现在该URI路径成分的内部 保留字符的百分号编码 a href E5 8F B9 E5 8F B7 html title 叹号 a a href Number sign html class mw redirect title Number sign a a href 24 html title a a href Ampersand html class mw redirect title Ampersand amp a a href E6 92 87 E5 8F B7 html title 撇号 a a href Bracket html class mw redirect title Bracket a a href Bracket html class mw redirect title Bracket a a href Asterisk html title Asterisk a a href E5 8A A0 E5 8F B7 E4 B8 8E E5 87 8F E5 8F B7 html title 加号与减号 a a href E9 80 97 E8 99 9F html title 逗號 a a href E6 96 9C E7 B7 9A html title 斜線 a a href E5 86 92 E5 8F B7 html title 冒号 a a href E5 88 86 E8 99 9F html title 分號 a a href Equal sign html class mw redirect title Equal sign a a href E9 97 AE E5 8F B7 html title 问号 a a href html title a a href Bracket html class mw redirect title Bracket a a href Bracket html class mw redirect title Bracket a 21 23 24 26 27 28 29 2A 2B 2C 2F 3A 3B 3D 3F 40 5B 5D在特定上下文中没有特殊含义的保留字符也可以被百分号编码 在语义上与不百分号编码的该字符没有差别 在URI的 查询 成分 字符后的部分 中 例如 仍然是保留字符但是没有特殊含义 除非一个特定的URI有其它规定 该 字符在没有特殊含义时不需要百分号编码 如果保留字符具有特殊含义 那么该保留字符用百分号编码的URI与该保留字符仅用其自身表示的URI具有不同的语义 对未保留字符的百分号编码 编辑 未保留字符不需要百分号编码 两个URI的差别如果仅在于未保留字符是用百分号编码还是用字符自身表示 那么这两个URI具有等价的语义 但URI处理器实际上并不总是把二者视作等价 來源請求 例如 URI的消费者不应该把 41 与 A 7E 与 视作不同 但是某些URI的消费者就是这么做了 为了最大的互操作性 URI的制造者不应该对未保留字符进行百分号编码 对百分号字符的百分号编码 编辑 由于百分号字符 表示百分号编码字节流的存在 因此百分号字符应该被编码为3个字节的序列 25 用于URI内部 对任意数据的百分号编码 编辑 大多数URI涉及表示任意数据 例如IP地址或文件系统路径作为URI的成分 二进数据 编辑 1994年发布的RFC 1738规定 1 URI中的二进制数据应该表示为8位元组的序列 然后对每个8位元组按照上述方式百分号编码 例如 字节值0F 十六进制 应表示为 0F 字节值41 十六进制 应表示为 A 或 41 优先使用未保留字符来表示这些字节值 因为这使得URL更短 字符数据 编辑 二进数据的百分号编码过程已经被外推到字符数据 甚至到不适合或未被完全规范的地步 在WWW初创阶段 仅仅处理ASCII字符是否编码问题 还没有什么问题 但随后发展到对非ASCII字符如何在URI中编码 缺少标准规范的情况下导致了歧义性的解释URI的错误 例如 基于RFC 1738与RFC 2396的协议规定 字符数据先要根据某种字符编码转换为字节流 然后再表示为URI 如果URI不提供是何种字符编码的提示信息 那么这个URI难以可靠的解析 当前标准 编辑 2005年1月发布的RFC 3986 建议所有新的URI必须对未保留字符不加以百分号编码 其它字符建议先转换为UTF 8字节序列 然后对其字节值使用百分号编码 此前的URI不受此标准的影响 非标准的实现 编辑 有一些不符合标准的把Unicode字符在URI中表示为 u i xxxx i 其中xxxx是用4个十六进制数字表示的Unicode的码位值 任何RFC都没有这样的字符表示方法 并且已经被W3C拒绝 页面存档备份 存于互联网档案馆 第三版的ECMA 262仍然包含函数escape var string var 使用这种语法 但也有函数encodeURI var uri var 转换字符到UTF 8字节序列并用百分号编码每个字节 application x www form urlencoded类型 编辑当HTML表单中的数据被提交时 表单的域名与值被编码并通过HTTP的GET或者POST方法甚至更古远的email 2 把请求发送给服务器 这里的编码方法采用了一个非常早期的通用的URI百分号编码方法 并且有很多小的修改如換行规范化以及把空格符的编码 20 替换为 按这套方法编码的数据的MIME类型是application x www form urlencoded 当前仍用于 虽然非常过时了 HTML与XForms规范中 此外 CGI规范包括了web服务器如何解码这类数据 利用这类数据的内容 如果发送的是HTTP GET请求 application x www form urlencoded数据包含在所请求URI的查询成分中 如果发送的是HTTP POST请求或通过email 数据被放置在消息体中 媒体类型的名字被包含在消息的Content Type头内部 参见 编辑Quoted printable参考文献 编辑 RFC 1738 2 2 RFC 2396 2 4 RFC 3986 1 2 1 2 1 2 5 User agent support for email based HTML form submission using a mailto URL as the form action was proposed in RFC 1867 section 5 6 during the HTML 3 2 era Various web browsers implemented it by invoking a separate email program or using their own rudimentary SMTP capabilities Although sometimes unreliable it was briefly popular as a simple way to transmit form data without involving a web server or CGI scripts 外部链接 编辑RFC 3986 STD 66 plus errata 页面存档备份 存于互联网档案馆 the current generic URI syntax specification RFC 2396 obsolete plus errata 页面存档备份 存于互联网档案馆 and RFC 2732 plus errata 页面存档备份 存于互联网档案馆 together comprised the previous version of the generic URI syntax specification RFC 1738 mostly obsolete and RFC 1808 obsolete which define URLs RFC 1630 obsolete the first generic URI syntax specification W3C Guidelines on Naming and Addressing URIs URLs 页面存档备份 存于互联网档案馆 W3C explanation of UTF 8 in URIs 页面存档备份 存于互联网档案馆 W3C HTML form content types 页面存档备份 存于互联网档案馆 在線URL解碼工具 永久失效連結 在線URL編碼器 永久失效連結 取自 https zh wikipedia org w index php title 百分号编码 amp oldid 69857771, 维基百科,wiki,书籍,书籍,图书馆,

文章

,阅读,下载,免费,免费下载,mp3,视频,mp4,3gp, jpg,jpeg,gif,png,图片,音乐,歌曲,电影,书籍,游戏,游戏。