Decode escaped Url without using HttpUtility.UrlDecode

前端 未结 8 1697
盖世英雄少女心
盖世英雄少女心 2020-12-05 09:52

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 r

相关标签:
8条回答
  • 2020-12-05 09:58

    EDIT: Use the static method Uri.UnescapeDataString() to decode your URLs:

    Encoded: 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=

    0 讨论(0)
  • 2020-12-05 09:58

    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").

    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.

    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.

    0 讨论(0)
  • 2020-12-05 10:00

    System.Net.WebUtility.HtmlDecode is also working on .NET 4.0 Client Profile.

    0 讨论(0)
  • 2020-12-05 10:11

    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:

    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

    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&param2=there@is<a space");
    var test2 = HttpUtility.UrlEncode("http://www.test.com/?param1=22&param2=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
    

    More information....

    0 讨论(0)
  • 2020-12-05 10:12

    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.

    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?

    So don't worry about it.

    0 讨论(0)
  • 2020-12-05 10:14

    @Smith
    I was having the save problem. No changes or just further jumbling.

    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.HtmlDecode and Uri.UnescapeDataString on the new string. For some reason I had to run the decode and unescape in the order I mentioned. Bizarre.

    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)
    
    0 讨论(0)
提交回复
热议问题