好吧,您链接到的文档是针对 IIS 6 Server.UrlEncode 的,但您的标题似乎询问了 .NET System.Web.HttpUtility.UrlEncode。使用 Reflector 之类的工具,我们可以看到后者的实现,并确定它是否符合 W3C 规范。
这是最终调用的编码例程(注意,它是为字节数组定义的,其他接受字符串的重载最终将这些字符串转换为字节数组并调用此方法)。您可以为每个控件名称和值调用它(以避免转义= &
用作分隔符的保留字符)。
protected internal virtual byte[] UrlEncode(byte[] bytes, int offset, int count)
{
if (!ValidateUrlEncodingParameters(bytes, offset, count))
{
return null;
}
int num = 0;
int num2 = 0;
for (int i = 0; i < count; i++)
{
char ch = (char) bytes[offset + i];
if (ch == ' ')
{
num++;
}
else if (!HttpEncoderUtility.IsUrlSafeChar(ch))
{
num2++;
}
}
if ((num == 0) && (num2 == 0))
{
return bytes;
}
byte[] buffer = new byte[count + (num2 * 2)];
int num4 = 0;
for (int j = 0; j < count; j++)
{
byte num6 = bytes[offset + j];
char ch2 = (char) num6;
if (HttpEncoderUtility.IsUrlSafeChar(ch2))
{
buffer[num4++] = num6;
}
else if (ch2 == ' ')
{
buffer[num4++] = 0x2b;
}
else
{
buffer[num4++] = 0x25;
buffer[num4++] = (byte) HttpEncoderUtility.IntToHex((num6 >> 4) & 15);
buffer[num4++] = (byte) HttpEncoderUtility.IntToHex(num6 & 15);
}
}
return buffer;
}
public static bool IsUrlSafeChar(char ch)
{
if ((((ch >= 'a') && (ch <= 'z')) || ((ch >= 'A') && (ch <= 'Z'))) || ((ch >= '0') && (ch <= '9')))
{
return true;
}
switch (ch)
{
case '(':
case ')':
case '*':
case '-':
case '.':
case '_':
case '!':
return true;
}
return false;
}
例程的第一部分计算需要替换的字符数(空格和非 url 安全字符)。例程的第二部分分配一个新缓冲区并执行替换:
- 网址安全字符保持原样:
a-z A-Z 0-9 ()*-._!
- 空格转换为加号
- 所有其他字符都转换为
%HH
RFC1738 状态(强调我的):
因此,只有字母数字、特殊字符“$-_.+!*'()”和
用于其保留目的的保留字符可以
在 URL 内未编码使用。
另一方面,不需要编码的
字符(包括字母数字)可以在
URL 的特定于方案的部分中编码,只要它们不被用于保留
目的。
允许的 URL 安全字符UrlEncode
集是 RFC1738 中定义的特殊字符的子集。即,字符丢失并且即使规范说它们是安全的$,
也会被编码。UrlEncode
由于它们可能未编码(而不是must)使用,它仍然符合编码它们的规范(第二段明确说明)。
关于换行符,如果输入有一个CR LF
序列,那么它将被转义%0D%0A
。但是,如果输入只有LF
then 将被转义%0A
(因此在此例程中没有对换行符进行规范化)。
底线:它在额外编码的同时满足规范$,
,调用者负责在输入中提供适当规范化的换行符。