[ACCEPTED]-Decode escaped Url without using HttpUtility.UrlDecode-url

Accepted answer
Score: 73

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=


Score: 7

If you are using .NET 4.0 or later, you 3 can use WebUtility.UrlDecode which works with client profile 2 and also correctly processes plus signs 1 (see this discussion).

Score: 4

Re not loading System.Web.dll - as others 12 have noted, it isn't worth getting excited 11 unless you know that you need to deal with 10 clients that might not have it ("client 9 profile", "compact framework", "micro framework", "silverlight").

Re 8 space; it won't be a lot really; note that 7 .NET assemblies are JITted on a method-by-method 6 basis, so there won't be any significant 5 overhead just from using a few methods.

The 4 real issue (IMO) is your level of confidence 3 that the client has System.Web.dll; if you 2 are happy that they are using the full framework, then 1 just go for it.

Score: 2

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

After testing 7 many things I noticed a test string did 6 decode. Ultimately I had to create a new 5 empty string, set it's value to the encoded 4 string then run WebUtility.HtmlDecode and Uri.UnescapeDataString on the new string. For 3 some reason I had to run the decode and 2 unescape in the order I mentioned. Bizarre.

I 1 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)
Score: 0

The Microsoft ACE team have an extended 4 (and better) version of decode, in the Anti-XSS library. However 3 I'm not sure if it just passes through.

(I 2 don't see why you're that worried about 1 the dependency on System.web.dll to be honest)

Score: 0

You already have a HUGE dependency on the 10 .NET framework, CLR etal. So, in fact, you 9 already have an indirect dependency on System.Web.DLL; your 8 application CAN NOT RUN without its presence 7 on the local machine.

And you're worried 6 about memory? Do you have memory issues? If 5 you have memory issues so extreme you can't 4 load a couple KBs of DLL into your app's 3 memory, then why are you coding .NET? Or 2 are you just prematurely optimizing?

So 1 don't worry about it.

Score: 0

Just a little understanding of why it's 3 different. One converts to uppercase and 2 one converts to lowercase. So the decode 1 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);
        return (char) (n - 10 + 65);

System.Web.Util.HttpEncoderUtility (Internal) - + 97

public static char IntToHex(int n)
    if (n <= 9)
        return (char) (n + 48);
        return (char) (n - 10 + 97);


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


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

More Related questions