Setting the charset of html response content to 1252

Mourndark picture Mourndark · Apr 24, 2014 · Viewed 8.7k times · Source

I'm trying to send some data encoded in Windows 1252 (it's a CSV file) in an HTTP response, but somewhere along the way it's getting re-encoded to UTF-8 (no BOM). How can I make sure that the data stays in the correct encoding?

var sb = new StringBuilder();
// Build the file from windows-1252 strings in the sb...
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.AddHeader("content-disposition", string.Format("filename=\"{0}\".csv", fileName));
HttpContext.Current.Response.ContentType = "text/csv;charset=windows-1252";
HttpContext.Current.Response.Charset = "windows-1252";
HttpContext.Current.Response.Write(sb.ToString());
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();

Answer

Richard picture Richard · Apr 24, 2014

When you call

HttpContext.Current.Response.Write(someString)

the input someString will be in .NET's internal string representation (actually UTF-16). To actually send the output this has to be converted. By default this conversion will be to UTF-8 (because it efficiently supports the whole of Unicode).

The Charset property simply sets the HTTP response headers. But not there is also a ContentEncoding property to actually control how strings are sent.

So you are missing

HttpContext.Current.Response.ContentEncoding = Encoding.GetEncoding("Windows-1252")

See the description of System.Text.Encoding for a list of supported encodings.