Thursday, September 25, 2008

Double URL Encoding

Sometimes you know the answer but you still want to see it happen to make yourself happy.

Question: If you encode some text and then encode it again is it encoded inside encoded?
Answer: Yes
Question: What happens if you decode it twice as well?
Answer: You get back to the original unecoded text

Here is a snippet of text that demonstrates it using C# in .NET. You need to include the System.Web library.

            string stringWithAmp = "&";
            Console.WriteLine(stringWithAmp); // "&"
            stringWithAmp = HttpUtility.HtmlEncode(stringWithAmp);
            Console.WriteLine(stringWithAmp); // "&"
            stringWithAmp = HttpUtility.HtmlEncode(stringWithAmp);
            Console.WriteLine(stringWithAmp); // "&"
            stringWithAmp = HttpUtility.HtmlDecode(stringWithAmp);
            Console.WriteLine(stringWithAmp); // "&"
            stringWithAmp = HttpUtility.HtmlDecode(stringWithAmp);
            Console.WriteLine(stringWithAmp); // "&"
 

No comments:

Post a Comment