关于 URI 编码 by cifer

View this thread on steempeak.com
· @cifer ·
$3.81
关于 URI 编码
<html>
<p>URI 编码</p>
<p>URI 中的一些字符被保留来用作分隔符, 比如 `/`, `:` 用来指明协议, `?` 用来分割 path 和 querystring, `&amp;` 用来分割 querystring 中的不同参数, `=` 用来分割每个参数的键和值. 其它还有很多.</p>
<p>那么问题来了, 如果我们想在 URI 中正常使用这些保留字符该怎么用呢? 比如说我们就是想在参数得值中包含 `&amp;` 字符:</p>
<p><a href="http://127.0.0.1:8080/index.php?key1=value1&amp;key2=value2&amp;value3">http://127.0.0.1:8080/index.php?key1=value1&amp;key2=value2&amp;value3</a></p>
<p>如果不做其他处理, 就这样直接请求这个 URI 的话, `key2=value2&amp;value3` 将会被当做两个参数, 其中一个参数键为 key2, 值为 value2, 另一个参数键为 value3, 没有值.</p>
<p>那么怎么办呢, RFC3986 已经给出了解决方法, 就是在我们想正常使用这些特殊字符的时候用 `%HH` 的方式编码它们.</p>
<h4>其它需要编码的字符</h4>
<p>RFC3986 中定义了保留字符, 这些字符必须被编码; 还定义了非保留字符, 这些字符完全没必要被编码, 但是你硬要编也可以. 最后剩下的字符不属于这两类, 但是也可能需要被编码. 比如 `%` 本身, 因为 `%` 被做编码的前缀了, 所以如果我们的数据中要包含 `%` 的话必须也对它编码.</p>
<h4>关于空格</h4>
<p>关于空格为什么需要被编码, 可以看这里:&nbsp;</p>
<p><a href="http://www.blooberry.com/indexdot/html/topics/urlencoding.htm">http://www.blooberry.com/indexdot/html/topics/urlencoding.htm</a></p>
<p>空格字符在 RFC3986 中规定是应该编码成 `%20` 的, 但是在 `application/x-www-form-urlencode` 这种内容类型中是被编码成 `+` 的. (但是仅限于 querystring 或者 post data: <a href="http://stackoverflow.com/questions/2678551/when-to-encode-space-to-plus-or-20">http://stackoverflow.com/questions/2678551/when-to-encode-space-to-plus-or-20</a>)</p>
</html>
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,
👎  
properties (23)
post_id67,520,274
authorcifer
permlinkuri
categorycn-programming
json_metadata{"links":["http:\/\/127.0.0.1:8080\/index.php?key1=value1&key2=value2&value3","http:\/\/www.blooberry.com\/indexdot\/html\/topics\/urlencoding.htm","http:\/\/stackoverflow.com\/questions\/2678551\/when-to-encode-space-to-plus-or-20"],"format":"html","app":"steemit\/0.1","tags":["cn-programming","programming","cn","cn-reader"]}
created2018-12-17 06:22:21
last_update2018-12-17 06:22:21
depth0
children1
net_rshares6,571,619,099,418
last_payout2018-12-24 06:22:21
cashout_time1969-12-31 23:59:59
total_payout_value2.900 SBD
curator_payout_value0.906 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length1,259
author_reputation7,963,630,681,040
root_title"关于 URI 编码"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
author_curate_reward""
vote details (61)
@mahaw ·
I 'm Myanmar. I want to fiiendship you.
properties (22)
post_id67,530,845
authormahaw
permlinkre-cifer-uri-20181217t122253417z
categorycn-programming
json_metadata{"app":"steemit\/0.1","tags":["cn-programming"]}
created2018-12-17 12:22:45
last_update2018-12-17 12:22:45
depth1
children0
net_rshares0
last_payout2018-12-24 12:22:45
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length39
author_reputation7,300,176,045,008
root_title"关于 URI 编码"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000