System.Web.HttpUtility.UrlEncode and character encodings

A kind reader of one of my blogs posed me a question about a problem with HttpUtility.UrlEncode. His problem was that he needed to URL encode São Marcos and instead of getting S%E3o%20Marcos he was getting S%C3%A3o%20Marcos.

The problem here is the character encoding. The default encoding used by HttpUtility.UrlEncode (and HttpUtility.UrlDecode) is utf-8 and the requested site is expecting iso-8859-1.

To solve this problem, all that is needed calling HttpUtility.UrlEncode with the required character encoding:

System.Web.HttpUtility.UrlEncode(“São Marcos”, System.Text.Encoding.GetEncoding(“iso-8859-1″))

4 thoughts on “System.Web.HttpUtility.UrlEncode and character encodings”

  1. wow that is amazing! i just downloaded Reflector to take a look. It is showing what is happening inside. that is a valuable tool. thanks for the info!

Comments are closed.