Response.CharSet Vs Response.ContentEncoding Vs responseEncoding property in web.Config

B

bruce barker

Response.ContentEncoding - specifies whether the data is ascii or a unicode
variant.
Response.CharSet - specifies what charset was used with the encoding.
(English, French, Chinese,etc), this is because two characters can be
encoded to the same value. this even happens with unicode, because the
pictographic languages are all mapped to a common set.

these are settable in your code behind. there are Page and web config
setting for these that you can use to set the default, or you can override
them in you code.


-- bruce (sqlwork.com)
 
J

Joerg Jooss

Calvin Lai said:
Does anyone know the difference and usage of them? Great thanks!

HttpResponse.ContentEncoding sets the Encoding object of the underlying
response stream *and* sets the charset of the content-type if
applicable. That's useful if you want to override the default encoding
specified in web.config.

HttpResponse.CharSet only sets the charset of the content-type and does
not effect the actual encoding applied to the output stream -- which
makes it of limited use as far as I'm concerned.

Note that nothing prevents you from doing stuff like
Response.ContentEncoding = Encoding.GetEncoding("Windows-1252");
Response.Charset = "UTF-8";

which is obviously utter nonsense, but sent to the browser anyway ;->

Cheers,
 
C

Calvin Lai

Thanks for the clarification. However one problem remaining:
If my web application now has the responseEncodig set as iso8859-1, and the
ChatSet of resposne set as "big5". The unicode data retrieved from database
has no problem displaying in the client side browser.

How could I transform (using Text.Encoding.Convert, GetString, Get Bytes
methods) the data so that the client can still display correctly if I
change the responseEncoding to big5? Is there any way? Thanks a lot. I have
tried so many diferent combinations (which might still be wrong tho). I
really need help here.

Calvin
 
C

Calvin Lai

Thanks for the clarification. However one problem remaining:
If my web application now has the responseEncodig set as iso8859-1, and the
ChatSet of resposne set as "big5". The unicode data retrieved from database
has no problem displaying in the client side browser.

How could I transform (using Text.Encoding.Convert, GetString, Get Bytes
methods) the data so that the client can still display correctly if I
change the responseEncoding to big5? Is there any way? Thanks a lot. I have
tried so many diferent combinations (which might still be wrong tho). I
really need help here.

Calvin
 
C

Calvin Lai

Here is a snipplet of the code that I used: (originalStirng containg some
chinese characters previously stored using iso8859-1)

System.Text.Encoding originalEncoding =
System.Text.Encoding.GetEncoding("iso8859-1");
System.Text.Encoding destEncoding =
System.Text.Encoding.GetEncoding("big5");
byte[] originalBytes =
originalEncoding.GetBytes(originalString.ToString());
byte[] destBytes =
System.Text.Encoding.Convert(originalEncoding, destEncoding, originalBytes);
Response.Write(destEncoding.GetString(destBytes) + "<BR>"); <--
Still doens't work. Same string retruned
 
J

Joerg Jooss

Calvin Lai said:
Thanks for the clarification. However one problem remaining:
If my web application now has the responseEncodig set as iso8859-1,
and the ChatSet of resposne set as "big5". The unicode data
retrieved from database has no problem displaying in the client side
browser.

As I said, only setting CharSet is nonsensical if the real encoding is
different from that.
How could I transform (using Text.Encoding.Convert, GetString, Get
Bytes methods) the data so that the client can still display
correctly if I change the responseEncoding to big5? Is there any
way? Thanks a lot. I have tried so many diferent combinations (which
might still be wrong tho). I really need help here.

Calvin,

I'm kind of lost. What are you're trying to achieve? Do you want to use
ISO-8859-1 as default response encoding and Big5 in special cases (or
vice versa)? Anyway, set the <globalization /> in web.config to the
desired default encoding and set ContentEncoding to the desired "special
case" encoding instance.

Cheers,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top