得到错误400/404 - HttpUtility.UrlEncode没有编码完整的字符串?

Jus*_*808 8 c# iis-6 urlencode

为什么以下URL会在下面给出IIS错误:

A)http://192.168.1.96/cms/View.aspx/Show/Small+test '

A2)http://192.168.1.96/cms/View.aspx/Show/Small%20test '< - 这是有效的,但不是来自HttpUtility.UrlEncode()的结果

B)http://192.168.1.96/cms/View.aspx/Show/'%26 $%23funky**!!〜''+ page

A错误:

HTTP Error 404.11 - Not Found
The request filtering module is configured to deny a request that contains a double escape sequence.
Run Code Online (Sandbox Code Playgroud)

B错误:

HTTP Error 400.0 - Bad Request
ASP.NET detected invalid characters in the URL.
Run Code Online (Sandbox Code Playgroud)

/ Show /之后的URL的最后一部分是通过HttpUtility.UrlEncode()发送文本后的结果,因此,根据Microsoft,它是正确的URL编码.

如果我使用HttpUtility.UrlPathEncode()而不是HttpUtility.UrlEncode(),我得到A2结果.但B最终看起来像:

http://192.168.1.96/TVCMS-CVJZ/cms/View.aspx/Show/'&$#funky**!!〜''%20page

仍然是错的.Microsoft是否知道如何对URL进行编码?是否有人编写过正确的方法来执行此操作?

编辑:

我写了自己的编码器:

static public string UrlEncode(string encode)
{
    if (encode == null) return null;
    string encoded = "";

    foreach (char c in encode)
    {
        int val = (int)c;
        if ((val >= 48 && val <= 57) || (val >= 65 && val <= 90) || (val >= 97 && val <= 122))
            encoded += c;
        else
            encoded += "%" + val.ToString("X");
    }

    return encoded;
}
Run Code Online (Sandbox Code Playgroud)

该功能适用​​于上面的A2就好了B的结果是:

http://192.168.1.96/cms/View.aspx/Show/%27%26%24%23funky%2A%2A%21%21~%27%27%20page

但即使看起来像一个很好的有效URL IIS 仍然给我一个

HTTP错误400.0 - 错误请求ASP.NET在URL中检测到无效字符.

Jus*_*808 6

好的,回答我自己的问题......讨厌这样做,但经过多次挖掘后我得到了答案.

http://www.lostechies.com/blogs/joshuaflanagan/archive/2009/04/27/asp-net-400-bad-request-with-restricted-characters.aspx

微软的长期和短期决定不再坚持国际标准.

%,&,*,或:不能在URL之前,编码或解码?无论如何.

为了解决这个问题,我编写了自己的编码和解码:

static public string UrlEncode(string encode)
{
    if (encode == null) return null;
    string encoded = "";

    foreach (char c in encode)
    {
        int val = (int)c;
        if (val == 32 || val == 45 || (val >= 48 && val <= 57) || (val >= 65 && val <= 90) || (val >= 97 && val <= 122))
            encoded += c;
        else
            encoded += "%" + val.ToString("X");
    }

    // Fix MS BS
    encoded = encoded.Replace("%25", "-25").Replace("%2A", "-2A").Replace("%26", "-26").Replace("%3A", "-3A");

    return encoded;
}

static public string UrlDecode(string decode)
{
    if (decode == null) return null;
    // Fix MS BS
    decode = decode.Replace("-25", "%25").Replace("-2A", "%2A").Replace("-26", "%26").Replace("-3A", "%3A");

    return HttpUtility.UrlDecode(decode);
}
Run Code Online (Sandbox Code Playgroud)

目前这两个函数都不是Unicode友好的,但现在它可以工作.