So basically, what I wanted was:
http%3A%2F%2Fwww.google.com
instead of
http%3a%2f%2fwww.google.com
I poked around a little and found that by default C# encodes URLs in lowercase. This is what you might call a limitation in C#. So I did the following work around:
I encoded the URL normally through System.Web.HttpUtility.UrlEncode()
And then wrote this simple function which finds the % in the encoded URL and simply converts the next 2 characters to uppercase:
Now, ucencode will have the URL encoded with Upper Case.char[] lcencode = System.Web.HttpUtility.UrlEncode(URL).ToCharArray();
for (int i = 0; i < lcencode.Length - 2; i++)
{
if (lcencode[i] == '%')
{
lcencode[i + 1] = char.ToUpper(lcencode[i + 1]);
lcencode[i + 2] = char.ToUpper(lcencode[i + 2]);
}
}
string ucencode = new string(lcencode);
1 comment:
Thanks. I have the same issue and thought of manually converting them too. I hoped there would be another way, but looks like that's not the case. Cheers
Post a Comment