URL 中存在空格?

发布于 2024-10-27 00:20:21 字数 498 浏览 1 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

丿*梦醉红颜 2024-11-03 00:20:21

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.

妳是的陽光 2024-11-03 00:20:21

他们确实是傻瓜。如果您查看 RFC 3986 附录 A,您会发现语法中根本没有提及“空格”用于定义 URL。由于语法中没有提及,因此对空格进行编码的唯一方法是使用百分比编码 (%20)。

事实上,RFC 甚至声明空格是分隔符,应该被忽略:

在某些情况下,额外的空白(空格、换行符、制表符等)可能会
必须添加以打破跨行的长 URI。空白
提取 URI 时应被忽略。

为了稳健性,接受用户输入 URI 的软件应该尝试
识别并去除分隔符和嵌入的空格。

奇怪的是,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:

In some cases, extra whitespace (spaces, line-breaks, tabs, etc.) may
have to be added to break a long URI across lines. The whitespace
should be ignored when the URI is extracted.

and

For robustness, software that accepts user-typed URI should attempt
to recognize and strip both delimiters and embedded whitespace.

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).

挽你眉间 2024-11-03 00:20:21

空格只需替换为“%20”,例如:

http://www.example.com/my% 20美丽%20页

Spaces are simply replaced by "%20" like :

http://www.example.com/my%20beautiful%20page

落在眉间の轻吻 2024-11-03 00:20:21

我认为那里的信息部分正确:

这不是真的。 URL 可以使用空格。没有任何定义将空格替换为 + 号。

正如您所指出的,URL 不能使用空格。 HTTP 请求将会被搞砸。我不确定 + 是在哪里定义的,尽管 %20 是标准的。

The information there is I think partially correct:

That's not true. An URL can use spaces. Nothing defines that a space is replaced with a + sign.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文