Tuesday, 19 April 2011

Upper Case URL Encoding in C#

Recently, I was faced with the following problem: I was cloning a HTTP call in C# and had to encode a URL in Upper case as the server was not accepting the URL encoded in lower case.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: