HttpUtility.UrlEncode 是否符合“x-www-form-urlencoded”的规范?
URLEncode 转换字符如下:
- 空格 ( ) 会转换为加号 (+)。
- 非字母数字字符将转义为其十六进制表示形式。
与 W3C
application/x-www-form-urlencoded
这是默认内容类型。使用此内容类型提交的表单必须按如下方式编码:
控件名称和值被转义。空格字符被替换 按“+”,然后保留字符 按照 RFC1738 中的描述进行转义, 第 2.2 节:非字母数字 字符被替换为 '%HH',a 百分号和两个十六进制 代表 ASCII 码的数字 角色。换行符是 表示为“CR LF”对(即 “%0D%0A”)。
控件名称/值按照它们在列表中出现的顺序列出。 文档。名字是分开的 '=' 和名称/值对的值 彼此之间用“&”分隔。
我的问题是,有没有人做过确定 URLEncode 是否生成有效的 x-www-form-urlencoded 数据的工作?
URLEncode converts characters as follows:
- Spaces ( ) are converted to plus signs (+).
- Non-alphanumeric characters are escaped to their hexadecimal representation.
Which is similar, but not exactly the same as W3C
application/x-www-form-urlencoded
This is the default content type. Forms submitted with this content type must be encoded as follows:
Control names and values are escaped. Space characters are replaced
by '+', and then reserved characters
are escaped as described in RFC1738,
section 2.2: Non-alphanumeric
characters are replaced by '%HH', a
percent sign and two hexadecimal
digits representing the ASCII code of
the character. Line breaks are
represented as "CR LF" pairs (i.e.,
'%0D%0A').The control names/values are listed in the order they appear in the
document. The name is separated from
the value by '=' and name/value pairs
are separated from each other by '&'.
My question is, has anyone done the work to determine whether URLEncode produces valid x-www-form-urlencoded data?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,您链接到的文档适用于 IIS 6 Server.UrlEncode,但您的标题似乎询问有关 .NET System.Web.HttpUtility.UrlEncode。使用Reflector这样的工具,我们可以看到后者的实现并确定它是否符合W3C规范。
这是最终调用的编码例程(注意,它是为字节数组定义的,以及采用字符串的其他重载最终将这些字符串转换为字节数组并调用此方法)。您可以为每个控件名称和值调用此方法(以避免转义用作分隔符的保留字符
= &
)。该例程的第一部分计算需要替换的字符数(空格和非 URL 安全字符)。该例程的第二部分分配一个新的缓冲区并执行替换:
az AZ 0-9 ()*-._!
%HH
RFC1738 状态(强调我的):
UrlEncode
允许的 Url 安全字符集是 RFC1738 中定义的特殊字符的子集。也就是说,字符$,
丢失,并且将由UrlEncode
编码,即使规范说它们是安全的。由于它们可以未编码地使用(而不是必须),因此它仍然符合对它们进行编码的规范(第二段明确指出了这一点)。对于换行符,如果输入具有
CR LF
序列,则该序列将被转义%0D%0A
。但是,如果输入只有LF
,那么它将被转义%0A
(因此此例程中没有换行符的标准化)。底线:它满足规范,同时另外编码
$,
,并且调用者负责在输入中提供适当规范化的换行符。Well, the documentation you linked to is for IIS 6 Server.UrlEncode, but your title seems to ask about .NET System.Web.HttpUtility.UrlEncode. Using a tool like Reflector, we can see the implementation of the latter and determine if it meets the W3C spec.
Here is the encoding routine that is ultimately called (note, it is defined for an array of bytes, and other overloads that take strings eventually convert those strings to byte arrays and call this method). You would call this for each control name and value (to avoid escaping the reserved characters
= &
used as separators).The first part of the routine counts the number of characters that need to be replaced (spaces and non- url safe characters). The second part of the routine allocates a new buffer and performs replacements:
a-z A-Z 0-9 ()*-._!
%HH
RFC1738 states (emphasis mine):
The set of Url Safe Characters allowed by
UrlEncode
is a subset of the special characters defined in RFC1738. Namely, the characters$,
are missing and will be encoded byUrlEncode
even when the spec says they are safe. Since they may be used unencoded (and not must), it still meets the spec to encode them (and the second paragraph states that explicitly).With respect to line breaks, if the input has a
CR LF
sequence then that will be escaped%0D%0A
. However, if the input has onlyLF
then that will be escaped%0A
(so there is no normalization of line breaks in this routine).Bottom Line: It meets the specification while additionally encoding
$,
, and the caller is responsible for providing suitably normalized line breaks in the input.