Search code examples
c#asp.netencodingutf-8utf-16

ASP.NET Response - UTF-16 encoded file but nothing shown in the browser


I am trying to send UTF-16BE text file as a HTTP Response from ASP.NET app.

Response.ContentType = "text/plain";
Response.ContentEncoding = System.Text.UnicodeEncoding.BigEndianUnicode;
Response.WriteFile(filename);

but nothing shown as the result and Fiddler doesn't show any encoding

enter image description here

is it me or web browsers don't like utf-16 text ?


Solution

  • No, your web browser is not the problem.

    This code:

    context.Response.ContentType = "text/html";
    context.Response.ContentEncoding = UnicodeEncoding.BigEndianUnicode;
    context.Response.Write("Hello World");
    

    Yields the expected content encoding:

    encoding

    But as soon as you use context.Response.WriteFile, the content encoding gets removed. Not sure if this is a feature or not. I assume the software on the other end has to determine the content encoding based on the output returned.