Asp Forum
Home
|
Login
|
Register
|
Search
Forums
>
microsoft.public.dotnet.framework.sdk
TextWriter/StreamWriter and UTF-8 Encoding (w/ ASP.Net Runtime
Rick Strahl
10/17/2003 9:49:00 PM
Hi all,
I'm working on some code that hosts the ASP.Net runtime in a WinForm app.
All works fine except one thing:
I can't seem to get the character encoding of the generated output from the
runtime correct. The runtime expects a TextWriter as input that it writes
to. By default the Runtime will write out UTF-8 encoded output. THis works
fine in Web apps obviously.
In my app, I write the output to file so I do:
loOutput = new StreamWriter(this.cOutputFile,false,Encoding.UTF8);
wwWorkerRequest Request = new wwWorkerRequest(Page, QueryString, loOutput);
HttpRuntime.ProcessRequest(Request);
But there are two problems. First the streamwriter doesn't write the UTF-8
prefix characters at teh beginning of the file which means IE or other
client app doesn't automatically treat the output as UTF-8.
Second the output is actually *double UTF 8* encoded! Any extended
characters that should normally be two characters wide encoded are 4
characters wide. Decoding these twice gets back the original. For example:
É¢ô
turns into:
Ãfâ?°Ã,¢Ãfâ?°
If I change the StreamWriter to use Encoding.Default (Codepage 1252) and
explicitly write out the prefix bytes it works for my simple test page:
loOutput = new
StreamWriter(this.cOutputFile,false,Encoding.GetEncoding(1252));
loOutput.Write("?");
Now IE correctly sees the page as UTF8 from disk and the characters are not
double UTF8 encoded.
I notice that this depends on how I have web.config configured. My real app
I'm rendering has Response encoding set to UTF8. If I change that to
Windows-1252 the app actually renders correct UTF-8 output!!!
It works, but it makes no sense to me... I can't say I understand what's
going on. Why am I getting double encoding if I use UTF8?
It seems to me I need to have a stream sent in that doesn't do any encoding
at all. But then how does ASP.Net natively do this
Can anybody shed some light on what I need to do to get a clean UTF-8 output
file?
TIA,
+++ Rick ---
--
Rick Strahl
West Wind Technologies
http://www.west...
http://www.west...
wwHelp
----------------------------------
Making waves on the Web
Servizio di avviso nuovi messaggi
Ricevi direttamente nella tua mail i nuovi messaggi per
TextWriter/StreamWriter and UTF-8 Encoding (w/ ASP.Net Runtime
Inserendo la tua e-mail nella casella sotto, riceverai un avviso tramite posta elettronica ogni volta che il motore di ricerca troverà un nuovo messaggio per te
Il servizio è completamente GRATUITO!
x
Login to ForumsZone
Login with Google
Login with E-Mail & Password