URL 中存在空格?
w3fools 声明 URL 可以包含空格: http://w3fools.com/#html_urlencode
这是真的吗? URL 如何包含未编码的空格?
我的印象是 HTTP 请求的请求行 使用空格作为分隔符,格式为{方法}{空间}{路径}{空间}{协议}
:
GET /index.html http/1.1
那么URL怎么能包含空格呢?如果可以的话,用+
替换空格的做法从何而来?
w3fools claims that URLs can contain spaces: http://w3fools.com/#html_urlencode
Is this true? How can a URL contain an un-encoded space?
I'm under the impression the request line of an HTTP Request uses a space as a delimiter, being formatted as {the method}{space}{the path}{space}{the protocol}
:
GET /index.html http/1.1
Therefore how can a URL contain a space? If it can, where did the practice of replacing spaces with +
come from?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
URL 不得包含文字空格。它必须使用 percent-encoding 或其他编码进行编码使用URL安全字符(例如application/x-www-form-urlencoded 使用
+
而不是%20
来表示空格)。但该说法正确还是错误取决于解释:从语法上讲,URI 不得包含文字空格,并且必须对其进行编码;从语义上讲,
%20
显然不是空格,但它代表一个空格。A URL must not contain a literal space. It must either be encoded using the percent-encoding or a different encoding that uses URL-safe characters (like application/x-www-form-urlencoded that uses
+
instead of%20
for spaces).But whether the statement is right or wrong depends on the interpretation: Syntactically, a URI must not contain a literal space and it must be encoded; semantically, a
%20
is not a space (obviously) but it represents a space.他们确实是傻瓜。如果您查看 RFC 3986 附录 A,您会发现语法中根本没有提及“空格”用于定义 URL。由于语法中没有提及,因此对空格进行编码的唯一方法是使用百分比编码 (
%20
)。事实上,RFC 甚至声明空格是分隔符,应该被忽略:
和
奇怪的是,RFC 中没有提到使用
+
作为空格编码,尽管它被保留为子分隔符。我怀疑它的使用要么只是约定,要么由不同的 RFC(可能是 HTTP)涵盖。They are indeed fools. If you look at RFC 3986 Appendix A, you will see that "space" is simply not mentioned anywhere in the grammar for defining a URL. Since it's not mentioned anywhere in the grammar, the only way to encode a space is with percent-encoding (
%20
).In fact, the RFC even states that spaces are delimiters and should be ignored:
and
Curiously, the use of
+
as an encoding for space isn't mentioned in the RFC, although it is reserved as a sub-delimeter. I suspect that its use is either just convention or covered by a different RFC (possibly HTTP).空格只需替换为“%20”,例如:
http://www.example.com/my% 20美丽%20页
Spaces are simply replaced by "%20" like :
http://www.example.com/my%20beautiful%20page
我认为那里的信息部分正确:
正如您所指出的,URL 不能使用空格。 HTTP 请求将会被搞砸。我不确定
+
是在哪里定义的,尽管%20
是标准的。The information there is I think partially correct:
As you noted, an URL can NOT use spaces. The HTTP request would get screwed over. I'm not sure where the
+
is defined, though%20
is standard.