HTTP 标头换行样式
哪种换行符样式更适合在 HTTP 标头中使用:\r\n
或 \n
,为什么?
Which line break style is preferable for use in HTTP headers: \r\n
or \n
, and why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
\r\n
,因为它在协议规范中被定义为换行符。 RFC2616 在第 2.2 节“基本规则”开头声明,相当明确地:RFC2616 在技术上已被 RFC7230 废弃,但它没有进行重大更改,并再次调用 CRLF 作为 第 3 节,并且 RFC 引用了 第 3 节。 rfc-editor.org/rfc/rfc5234#appendix-B.1" rel="noreferrer">RFC5234,附录 B.1 将“CRLF”定义为
%x0D %x0A
。然而,认识到人们出于任何目的都会违反标准,第 19.3 节(请注意,它重复了正确的序列):
在较新的 RFC7230,§ 3.5 中
因此,除非您想作恶或以其他方式违反 RFC 规则,否则请使用
\r\n
。\r\n
, because it's defined as the line break in the protocol specification. RFC2616 states at the beginning of section 2.2, "Basic Rules", quite unambiguously:RFC2616 was technically obsoleted by RFC7230, but it makes no drastic changes and again calls out CRLF as the delimiter in section 3, and that RFC references RFC5234, Appendix B.1 to define "CRLF" as
%x0D %x0A
.However, recognizing that people will break the standard for whatever purposes, there is a "tolerance provision" in section 19.3 (note that it re-iterates the correct sequence):
In the newer RFC7230, § 3.5
Therefore, unless you want to be Evil or otherwise break the RFC's rules, use
\r\n
.\r\n
因为 RFC 2616 是这么说的(第 2.2 节“基本规则”):\r\n
because RFC 2616 says so (Section 2.2, "Basic Rules"):CRLF ("\r\n"),因为浏览器遵循 RFC2616。
CRLF ("\r\n"), because browsers follow RFC2616.