.net 在不使用 HttpUtility.UrlDecode 的情况下解码转义的 Url
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/239567/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Decode escaped Url without using HttpUtility.UrlDecode
提问by huseyint
Is there any function that converts an escaped Url string to its unescaped form? System.Web.HttpUtility.UrlDecode()can do that job but I don't want to add a reference to System.Web.dll. Since my app is not a web application, I don't want to add a dependency for only using a function in an assembly.
是否有任何函数可以将转义的 Url 字符串转换为其非转义形式?System.Web.HttpUtility.UrlDecode()可以完成这项工作,但我不想添加对System.Web.dll. 由于我的应用程序不是 Web 应用程序,因此我不想添加仅在程序集中使用函数的依赖项。
UPDATE:Check Rick Strahl's blog postabout the same issue.
更新:查看Rick Strahl关于同一问题的博客文章。
回答by Igal Tabachnik
EDIT:Use the static method Uri.UnescapeDataString()to decode your URLs:
编辑:使用静态方法Uri.UnescapeDataString()解码您的网址:
Encoded:http%3a%2f%2fwww.google.com%2fsearch%3fhl%3den%26q%3dsomething%20%2323%26btnG%3dGoogle%2bSearch%26aq%3df%26oq%3d
编码:http%3a%2f%2fwww.google.com%2fsearch%3fhl%3den%26q%3dsomething%20%2323%26btnG%3dGoogle%2bSearch%26aq%3df%26oq%3d
Decoded:http://www.google.com/search?hl=en&q=something #23&btnG=Google+Search&aq=f&oq=
解码:http://www.google.com/search?hl=en&q=something #23&btnG=Google+Search&aq=f&oq=
回答by Lirrik
If you are using .NET 4.0 or later, you can use WebUtility.UrlDecodewhich works with client profile and also correctly processes plus signs (see thisdiscussion).
如果您使用的是 .NET 4.0 或更高版本,则可以使用WebUtility.UrlDecode,它适用于客户端配置文件并正确处理加号(请参阅此讨论)。
回答by Marc Gravell
Re not loading System.Web.dll - as others have noted, it isn't worth getting excited unless you know that you need to deal with clients that might not have it ("client profile", "compact framework", "micro framework", "silverlight").
重新加载 System.Web.dll - 正如其他人所指出的,除非您知道需要处理可能没有它的客户端(“客户端配置文件”、“紧凑框架”、“微框架”),否则不值得兴奋”、“银光”)。
Re space; it won't be a lot really; note that .NET assemblies are JITted on a method-by-method basis, so there won't be any significant overhead just from using a few methods.
重新空间;真的不会很多;请注意,.NET 程序集是在逐个方法的基础上进行 JIT 的,因此仅使用几种方法不会产生任何重大开销。
The real issue (IMO) is your level of confidence that the client has System.Web.dll; if you are happy that they are using the full framework, then just go for it.
真正的问题 (IMO) 是您对客户端具有 System.Web.dll 的信心;如果您对他们使用完整的框架感到高兴,那就去做吧。
回答by DaveCS
@Smith
I was having the save problem. No changes or just further jumbling.
@Smith
我遇到了保存问题。没有变化或只是进一步混乱。
After testing many things I noticed a test string did decode. Ultimately I had to create a new empty string, set it's value to the encoded string then run WebUtility.HtmlDecodeand Uri.UnescapeDataStringon the new string. For some reason I had to run the decode and unescape in the order I mentioned. Bizarre.
在测试了很多东西后,我注意到一个测试字符串确实解码了。最后,我不得不创建一个新的空字符串,它的值设置为编码字符串,然后运行WebUtility.HtmlDecode,并Uri.UnescapeDataString在新的字符串。出于某种原因,我不得不按照我提到的顺序运行 decode 和 unescape。奇怪。
I solved it with something like this.
我用这样的东西解决了它。
Dim strEncoded as string="http%3a%2f%2fwww.google.com%2fsearch%3fhl%3den%26q%3dsomething%20%2323%26btnG%3dGoogle%2bSearch%26aq%3df%26oq%3d"
Dim strDecoded as string = ""
strDecoded = strEncoded
strDecoded = WebUtility.HtmlDecode(strDecoded)
strDecoded = Uri.UnescapeDataString(strDecoded)
回答by blowdart
The Microsoft ACE team have an extended (and better) version of decode, in the Anti-XSS library. However I'm not sure if it just passes through.
Microsoft ACE 团队在Anti-XSS 库中有一个扩展(更好)的解码版本。但是我不确定它是否只是通过。
(I don't see why you're that worried about the dependency on System.web.dll to be honest)
(老实说,我不明白你为什么那么担心对 System.web.dll 的依赖)
回答by blowdart
You already have a HUGE dependency on the .NET framework, CLR etal. So, in fact, you already have an indirect dependency on System.Web.DLL; your application CAN NOT RUN without its presence on the local machine.
您已经非常依赖 .NET 框架 CLR 等。所以,实际上,您已经对 System.Web.DLL 有了间接依赖;如果本地机器上不存在您的应用程序,则无法运行。
And you're worried about memory? Do you have memory issues? If you have memory issues so extreme you can't load a couple KBs of DLL into your app's memory, then why are you coding .NET? Or are you just prematurely optimizing?
你担心内存?你有记忆问题吗?如果您的内存问题如此严重,您无法将几 KB 的 DLL 加载到应用程序的内存中,那么您为什么要编码 .NET?或者你只是过早地优化?
So don't worry about it.
所以不用担心。
回答by Switch
Just a little understanding of why it's different. One converts to uppercase and one converts to lowercase. So the decode is specific to the type of encode.
只是对为什么它不同的一点理解。一种转换为大写,一种转换为小写。所以解码是特定于编码类型的。
System.Net.WebUtility(Internal) + 65:
System.Net.WebUtility(内部)+ 65:
private static char IntToHex(int n)
{
if (n <= 9)
return (char) (n + 48);
else
return (char) (n - 10 + 65);
}
System.Web.Util.HttpEncoderUtility(Internal) - + 97
System.Web.Util.HttpEncoderUtility(内部) - + 97
public static char IntToHex(int n)
{
if (n <= 9)
return (char) (n + 48);
else
return (char) (n - 10 + 97);
}
Example:
示例:
var test1 = WebUtility.UrlEncode("http://www.test.com/?param1=22¶m2=there@is<a space");
var test2 = HttpUtility.UrlEncode("http://www.test.com/?param1=22¶m2=there@is<a space");
Response:
回应:
test1 -> http%3A%2F%2Fwww.test.com%2F%3Fparam1%3D22%26param2%3Dthere%40is%3Ca+space
test2 -> http%3a%2f%2fwww.test.com%2f%3fparam1%3d22%26param2%3dthere%40is%3ca+space
回答by Zibri
System.Net.WebUtility.HtmlDecodeis also working on .NET 4.0 Client Profile.
System.Net.WebUtility.HtmlDecode还致力于 .NET 4.0 Client Profile。

