I have a client that uses % in their URLs. This is possible and normally this implies a hex number in the url. However… it is possible To have a URL like ttis:
GET https://localhost:46780/puzzel/Legering van ijzer met 14% silicium
In this case, UrlEncoding gives:
Legering%20van%20%C4%B3zer%20met%2014%%20silicium
Which is fine. However, the decoder assumes two digits after the % sign.
Not sure if this is an OK fix, but this seems to do the trick:
public static string UrlDecode(string text)
{
if (String.IsNullOrEmpty(text)) return null;
var bytes = new List<byte>();
var textLength = text.Length;
for (var i = 0; i < textLength; i++)
{
var c = text[i];
if (c == '+')
{
bytes.Add(32);
}
else if (c == '%' && text[i + 1] != '%') // test for additional % sign
{
var hexNo = Convert.ToByte(text.Substring(i + 1, 2), 16);
bytes.Add(hexNo);
i += 2;
}
else
{
bytes.Add((byte)c);
}
}
byte[] byteArray = bytes.ToArray();
return Encoding.UTF8.GetString(byteArray, 0, byteArray.Length);
}