错误400/404-HttpUtility.UrlEncode无法编码完整字符串吗? [英] Getting error 400 / 404 - HttpUtility.UrlEncode not encoding full string?
问题描述
为什么以下URL会给我以下IIS错误:
Why do the following URLs give me the IIS errors below:
A) http://192.168.1.96/cms/View.aspx/显示/小试+ '
A2) http://192.168.1.96/cms/View.aspx/Show/Small%20test '<-有效,但不是HttpUtility.UrlEncode()的结果
A2) http://192.168.1.96/cms/View.aspx/Show/Small%20test' <-- this works, but is not the result from HttpUtility.UrlEncode()
B) http://192.168.1.96/cms/View.aspx/Show/'%26 $%23funky ** !!〜''+页面
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.
B错误:
HTTP Error 400.0 - Bad Request
ASP.NET detected invalid characters in the URL.
/Show/之后的URL的最后一部分是通过HttpUtility.UrlEncode()发送文本后的结果,因此,根据Microsoft的说法,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.
如果我使用HttpUtility.UrlPathEncode()而不是HttpUtility.UrlEncode(),则会得到A2结果.但是B最终看起来像这样:
If I user HttpUtility.UrlPathEncode() rather than HttpUtility.UrlEncode() I get the A2 results. But B ends up looking like:
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
这是 still 错误. Microsoft完全不知道如何进行URL编码吗?有人编写了可以正确执行此功能的功能吗?
which is still wrong. Does Microsoft know how to URL Encode at all? Is there a function someone has written up to do it the correct way?
我已经编写了自己的编码器:
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;
}
该函数可以在上面的A2上正常工作,B的结果是:
The function works with A2 above just fine the result for B is:
http://192.168.1.96/cms/View.aspx/Show/%27%26%24%23funky%2A%2A%21%21~%27%27%20page
但是,即使它看起来像一个不错的有效URL IIS, still 也给了我一个
But even though that looks like a nice valid URL IIS still gives me a
HTTP错误400.0-错误的请求 ASP.NET在URL中检测到无效字符.
HTTP Error 400.0 - Bad Request ASP.NET detected invalid characters in the URL.
推荐答案
好,回答我自己的问题...讨厌这样做,但经过大量挖掘后我得到了答案.
OK, answering my own question... hate doing it but I got the answer after much digging.
总而言之,微软在其所有荣耀中决定不再坚持国际标准.
The long and short of it is the Microsoft in all its glory decided not to stick to a international standard, again.
%,&,*或:不能在URL中,不能在?之前进行编码或解码.出于任何原因.
%, &, *, or : can not be in a URL, encoded or decoded before a ? 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);
}
目前,这两个函数都不支持Unicode,但目前可以使用.
Neither of the functions are Unicode friendly at the moment, but for now it works.
这篇关于错误400/404-HttpUtility.UrlEncode无法编码完整字符串吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!