Reputation: 3084
strong textGiven a string that has been encoded n times, how can I decode the string to give me the plain unencoded string if I don't know the value of n.
To clarify:
Initial : /unencoded?string/
Encoded Once : %2Funencoded%3Fstring%2F
Encoded Twice: %252Funencoded%253Fstring%252F
Encoded Three Times: %25252Funencoded%25253Fstring%25252F
How do I get from %25252Funencoded%25253Fstring%25252F
to /unencoded?string/
without knowing it's been encoded three times?
I know I can use HttpServerUtility.UrlDecode
or similar, but this only decodes once.
Upvotes: 1
Views: 707
Reputation: 157
Updated .NET version of hometoast's answer below.
Essentially just swap HttpServerUtility.UrlDecode
with HttpUtility.UrlDecode
string encodedString = "....";
string temp = string.Empty;
string decodedString = HttpUtility.UrlDecode(encodedString);
while (decodedString != temp)
{
temp=decodedString;
decodedString = HttpUtility.UrlDecode(temp);
}
Upvotes: 2
Reputation: 11782
decode it until decoding doesn't change it any more.
string encodedString = "....";
string temp = string.Empty;
string decodedString = HttpServerUtility.UrlDecode(encodedString);
while (decodedString != temp)
{
temp=decodedString;
decodedString = HttpServerUtility.UrlDecode(temp);
}
Upvotes: 4