Paulo Morgado

.NET Development & Architecture

This Blog

Syndication

Search

Sponsored By

Tags

News

Unit Test Today! Get Typemock Isolator!

Books

 

Events

Visitors

Visitor Locations

Community

Email Notifications

Archives

Profile

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"))

Published Sun, May 6 2007 22:09 by Paulo Morgado

Comments

# System.Web.HttpUtility.UrlEncode and character encodings@ Sunday, May 06, 2007 5:05 PM

You've been kicked (a good thing) - Trackback from DotNetKicks.com

DotNetKicks.com

# re: System.Web.HttpUtility.UrlEncode and character encodings@ Sunday, March 02, 2008 4:59 AM

how do you know that default is UTF8?  i cannot find this information in msdn.

Yener

# re: System.Web.HttpUtility.UrlEncode and character encodings@ Monday, March 03, 2008 5:18 AM

Reflector is your friend. :)

I've entered community comments to documentation:

msdn2.microsoft.com/.../adwtk1fy.aspx

msdn2.microsoft.com/.../4fkewx0t.aspx

msdn2.microsoft.com/.../3221zfcx.aspx

msdn2.microsoft.com/.../c5y5x63c.aspx

Paulo Morgado

# re: System.Web.HttpUtility.UrlEncode and character encodings@ Tuesday, March 04, 2008 12:38 PM

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!

Yener

# re: System.Web.HttpUtility.UrlEncode and character encodings@ Tuesday, March 04, 2008 6:02 PM

I don't know what version you donwloaded, but I just noticed 5.1 is out.

Paulo Morgado

Leave a Comment

(required) 
(required) 
(optional)
(required)