简体   繁体   English

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

[英]Getting error 400 / 404 - HttpUtility.UrlEncode not encoding full string?

Why do the following URLs give me the IIS errors below: 为什么以下URL会在下面给出IIS错误:

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

A2) http://192.168.1.96/cms/View.aspx/Show/Small%20test ' <-- this works, but is not the result from HttpUtility.UrlEncode() 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 B) http://192.168.1.96/cms/View.aspx/Show/'%26 $%23funky ** !!〜''+ page

Error for A: A错误:

HTTP Error 404.11 - Not Found
The request filtering module is configured to deny a request that contains a double escape sequence.

Error for B: B错误:

HTTP Error 400.0 - Bad Request
ASP.NET detected invalid characters in the URL.

The last part of the URL after /Show/ is the result after the text is being sent through HttpUtility.UrlEncode() so, according to Microsoft it is URL Encoded correctly. / Show /之后的URL的最后一部分是通过HttpUtility.UrlEncode()发送文本后的结果,因此,根据Microsoft,它是正确的URL编码。

If I user HttpUtility.UrlPathEncode() rather than HttpUtility.UrlEncode() I get the A2 results. 如果我使用HttpUtility.UrlPathEncode()而不是HttpUtility.UrlEncode(),我得到A2结果。 But B ends up looking like: 但B最终看起来像:

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

which is still wrong. 仍然是错的。 Does Microsoft know how to URL Encode at all? Microsoft是否知道如何对URL进行编码? Is there a function someone has written up to do it the correct way? 是否有人编写过正确的方法来执行此操作?

EDIT: 编辑:

I've written my own encoder: 我写了自己的编码器:

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;
}

The function works with A2 above just fine the result for B is: 该功能适用​​于上面的A2就好了B的结果是:

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

But even though that looks like a nice valid URL IIS still gives me a 但即使看起来像一个很好的有效URL IIS 仍然给我一个

HTTP Error 400.0 - Bad Request ASP.NET detected invalid characters in the URL. HTTP错误400.0 - 错误请求ASP.NET在URL中检测到无效字符。

OK, answering my own question... hate doing it but I got the answer after much digging. 好的,回答我自己的问题......讨厌这样做,但经过多次挖掘后我得到了答案。

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

The long and short of it is the Microsoft in all its glory decided not to stick to a international standard, again. 微软的长期和短期决定不再坚持国际标准。

%, &, *, or : can not be in a URL, encoded or decoded before a ? %,&,*,或:不能在URL之前,编码或解码? for any reason. 无论如何。

To get around this I've written my own encode and decode: 为了解决这个问题,我编写了自己的编码和解码:

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);
}

Neither of the functions are Unicode friendly at the moment, but for now it works. 目前这两个函数都不是Unicode友好的,但现在它可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM