为什么即使参数未经过URL编码,某些查询字符串仍然有效? [英] Why do some query strings work even if parameters are not URL-encoded?
问题描述
这是一个例子:
The
url
parameter,http://journals.plos.org/plosone/s/file?id=wjVg/PLOSOne_formatting_sample_main_body.pdf
, is not encoded. It contains reserved characters, like the colon, slashes, and question mark.为什么这仍然有效?如果不使用编码,为什么还要麻烦编码呢?
Why does this still work? And why bother encoding if it works without it?
推荐答案
保留的字符
通常用作定界符-这并不意味着它们可能不被使用,仅意味着它们具有特殊用途,并且如果您不需要此用途,则必须对它们进行百分比编码. The reserved characters of an URI are mostly used as delimiters -- it doesn’t mean that they may not be used, it only means that they have a special purpose, and if you don’t need them for this purpose, you have to percent-encode them.
查询组件以第一个
?
开头,以第一个#
结尾(如果有的话,否则以URI的结尾).对于查询组件本身,没有定义保留字符.The query component starts with the first
?
and ends with the first#
(if any, otherwise with the end of the URI). For the query component itself, there are no reserved characters defined.URI标准RFC 3986 定义查询组件可以包含这些字符:
The URI standard RFC 3986 defines that the query component can contain these characters:
a
-z
,A
-Z
0
-9
/
?
:
@
!
$
&
'
(
)
*
+
,
;
=
.
_
~
- 百分号字符
a
-z
,A
-Z
0
-9
/
?
:
@
!
$
&
'
(
)
*
+
,
;
=
-
.
_
~
- percent-encoded characters
它甚至明确提到:
斜杠("/")和问号(?")可能表示查询组件中的数据.
The characters slash ("/") and question mark ("?") may represent data within the query component.
示例URI的查询组件是这样的:
The query component of your example URI is this:
embedded=true&url=http://journals.plos.org/plosone/s/file?id=wjVg/PLOSOne_formatting_sample_main_body.pdf
除字母外,它还包含
=
,&
,:
,/
,.
,?
,_
,所有这些在查询中都是允许的.Apart from letters, it contains
=
,&
,:
,/
,.
,?
,_
, all of which are allowed in the query.请注意,查询组件中的
name=value
格式(由&
分隔)只是一个惯例,而不是规范中定义的内容.Note that the
name=value
format (separated by&
) in the query component is just a convention, not something defined in the specification.这篇关于为什么即使参数未经过URL编码,某些查询字符串仍然有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!