473,388 Members | 1,352 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Set Request.ContentEncoding from code?

Hello NG,

I'm having some encoding trouble using my own RequestHandler:

Since the RequestHandler must be able to serve content with diffrent
encoding, I set the Response.Charset and Response.ContentEncoding to the
encoding of current content. Furthermore I set the Content-Type META tag of
the html page to use the same charset as well.

And this all works fine... I get my pages served and both the HTTP headers
and the HTML page has the right encoding applied... Yieppie!!!

But then... when I try to submit a FORM on one of my pages, all my special
characters (like "דרוצהי") gets lost in the process.

The only way I can preserve the special characters, is by setting the
requestEncoding attribute (specified in <globalization> in web.config) to
the same encoding as I the current Response.ContentEncoding. And that is a
bad solution since all of my requests could be in different encodings.

It doesn't work if i try to set the Response.ContentEncoding to the same as
the Request.ContentEncoding. It's as if the Request already has been decoded
using the wrong format (specified in <globalization> in web.config).

Can anyone please help? I'm I on the wrong track? Is it impossible to serve
diffrent request/response encodings through the same web site? Or am I just
doing it the wrong way?

Thanks for your time,

Ricky
Nov 18 '05 #1
8 5278
Hi Ricky,

From your description, you have met some problem manually setting the
Request's ContentEncoding in ASP.NET via code at runtime, you found it only
works when you specifying them in web.config however what you want it set
different value for each comming request rather than all the same in
web.config , yes?

As for this problem, here are my understanding and suggestions:
The Request/Response 's ContentEncoding set in web.config's Globalization
section is the default setting for the asp.net webapplication. Generally
the comming webrequest is using the encoding from the client(default
setting in browser), if there is no from client, the default setting in
web.config will be used. And also, the Asp.net 's Request and Response
object ContentEncoding can help use manually set them via code. And the
Response's ContentEncoding is nothing particular that we can just set it
before the response content return to client. However, the Request's
ContentEncoding will be a bit different , because when the request comming
, the asp.net runtime will soon checking the ContentEncoding (from client ,
if not, use the default in web.config), So if we set it later in Page's
processing life cycle, it's too late. We must set it before the request is
being processing. In ASP.NET there're several Events during each request's
processing , such as BeginRequest, AuthenticateRequest, EndRequest ...
And the "BeginREquest" is exactly the one we have to use, you can hook this
event in the application 's Global object(Global.asax.cs) or make a custom
HttpModule to handle it. Here are some related reference in MSDN:

#HttpApplication.BeginRequest Event
http://msdn.microsoft.com/library/en...webhttpapplica
tionclassbeginrequesttopic.asp?frame=true

#Custom HttpModule Example
http://msdn.microsoft.com/library/en...tomhttpmodules.
asp?frame=true

Also, following is a former thread discussing on a similiar issue, you may
also have a look:
#query string encoding/decoding
http://groups.google.com/groups?hl=e...readm=PLNLjDdA
EHA.1288%40cpmsftngxa06.phx.gbl&rnum=1&prev=/groups%3Fhl%3Den%26lr%3D%26ie%3
DUTF-8%26oe%3DUTF-8%26q%3Dquerystring%2Bsteven%2Bcheng

Hope helps. Thanks.
Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx

Nov 18 '05 #2
Thanks for you reply Steven. It works just fine setting the
Request.ContentEncoding in the Application BeginRequest event.
Now my only trouble is how do I select the right encoding for the Request?
Please let me elaborate:

1. I have a RequestHandler that serves content using diffrent encoding. So
when a danish page is being served the "ISO-8859-1" encoding is used by
setting the Response.ContentEncoding property accordingly.

2. The problem occurrs when a user submits form data from a page using
encoding other than the default defined in web.config (usually "utf-8").
Since the request is always beeing decoded using the defaut encoding this
results in characters beeing lost in the process.

3. The solution is to set the Request.ContentEncoding in the Application
BeginRequest event. But how do i know what encoding to use in this context?
Are there any way to tell which encoding the browser used when sending the
request?

Since I have to tell the browser which encoding to use in both the HTTP
Header and the HTML Content-Type Meta tag, the least it could do was to
return the favour and tell me which encoding it used when POSTing data to
me.

Thanks for your time,
Ricky
Nov 18 '05 #3
Hi Ricky,

Thanks for your followup. As for the new question, here are my
understandings:

Generally, all the infos we can ge from the client are all set in the Http
Request 's Header area , the Request object's
UserAgent, Request.ContentType,Request.Browser... etc
We can use the following code to loop through the comming request's header
collection:
foreach(string key in Request.Headers.Keys)
{
Response.Write("<br>"+key+ " : " + Request.Headers[key]);
}

There are some optional items such ad
ACCEPT-LANGUAGE
ACCEPT-ENCODING
ACCEPT-CHARSET

which indicate what kind of language/encoding type / charset the client
browser support. But I've not found anything which directly tell us what
kind of encoding the clieht is using. I think this is determined by the
client machine's OS. And for those new OS such as NT or XP which support
unicode will automatically use the UTF-8 or the certain charset
/contenttype from the serveside when first time getting the page from
server. I think on those old OS such as win98 which doesn't use Unicode may
have different behavior, you may have a test.

Also, you can use the above means to loop the Request's header collection
to see whether there is anyitem which is useful for your determination of
the contentEncoding to change in BeginRequest. If not, I'm afraid we
haven't any means to get anyother clientside's info so far.

Please have a check and if you have any questions, please feel free to post
here. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx



Nov 18 '05 #4
Hi Ricky,

Have you had a chance to check out the suggestions in my last reply or have
you got any further ideas on this issue? If you have anything unclear or if
there're anything else we can help, please feel free to post here. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx

Nov 18 '05 #5
Hi Steven,

Please forgive me for forgetting to write back with an update on my issue.

I tried looking at the Request object and the informatin found in the
Headers collection, but it provided me with no usefull information when
deciding what encoding should be applied to it.

The most usefull header must be the ACCEPT-CHARSET, but IE never provided me
with this one and Mozilla only returned a list of supported encoding
formats.

I guess this proves that you can't really use multiple encodings on one
ASP.NET application.

Cause as soon as you set the Response Encoding you'll need to decode
subsequent requests as well by applying the right ContentEncoding. And since
this can only be done in the Applications BegingRequest event (before any
process of the request, and creation of session) you'll have no idea what
encoding to use.

I'm still a bit frustrated about it, but for know my solution has been to
duplicate my ASP.NET application to multiple IIS websites and then applied
diffrent encoding to each of these by setting it in the web.config. Anyone
should be able to see that this is a bad solution.

Thanks for your time,
Ricky
Nov 18 '05 #6
Hi Ricky,

Thanks for the followup. Generally all the existing browsers won't send the
content-encoding header in request , some will send the ACCEPT-CHARSET but
which have nothing to do with the actual request or response's content. And
there also nothing on the contentencoding in the W3C's http specification.
So the problem is exising on all the browsers since the browser will
encoded the posted request's content. Here is a certain http1.1 content
encoding reference:

http://i4net.tv/marticle/mod_gzip/encoding.htm

Also, since the UTF-8 is currently the most widely used encoding type which
can represent all the datas from different district. So most browser and
serverside web application use this as the default encoding to process the
request's content. Anyway, I'll consult some further experts to see whether
they have any ideas on this issue and will update you if I got any addition
infos. Thanks.
Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx

Nov 18 '05 #7
Hi Ricky,

Regarding on the problem, I'm still consulting and will update you as soon
as possible. Thanks for your understanding.

Regards,

Steven Cheng
Microsoft Online Support

Nov 18 '05 #8
Hi Ricky,

I'm sorry for keeping you waiting for so long time. After further
consulting on some experts, they also confirm that the standard request
dosn't contain such a field which represent the request data's encoding
charset. And we could not get the exactly encoding charset from any header
items but looking at the bytes. And they also suggest that the
ACCEPT- LANGUAGES header is helpful on determining the encoding charset to
use, just check this value in BeginRequest event and set the appropriate
ContentEncoding. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx

Nov 18 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Allaigre David | last post by:
I'm trying to retreive some information from the querystring. The web site is as default configure in "utf-8". But on only one page i need to change dynamicly the contentEncoding to retreive...
1
by: Ricky K. Rasmussen | last post by:
Hello NG, I'm having some encoding trouble using my own RequestHandler: Since the RequestHandler must be able to serve content with diffrent encoding, I set the Response.Charset and...
6
by: Calvin Lai | last post by:
Does anyone know the difference and usage of them? Great thanks!
2
by: Wootaek Choi | last post by:
Hi everybody. I have a webapplication that use UTF-8 for request & response encoding. but.. One aspx page should get request with other encoding(ks_c_5601-1987), and response with utf-8 (...
0
by: Per Bolmstedt | last post by:
In my web.config, the default requestEncoding is set to X. However, I have a web form to which I know that Y encoded data will sometimes be POSTed. This data will be garbled when accessed...
1
by: tacoturtle | last post by:
Hello All, do all the properties that the request posses directly correlate to a http header variable? The reason I ask is that I am trying code against a scenario where the header variables do...
3
by: Axel Dahmen | last post by:
Hi, I've created an aspx page taking a query string argument as parameter. Although I've correctly escaped umlauts in the query string, they do not appear in the QueryString collection. If I...
1
by: Paul Phillips | last post by:
Has anyone been able or is having trouble with setting the Content-Encoding to a value in the headers collection of an HTTPWebRequest object. I am trying to post a RosettaNet message to a trading...
2
by: MDANH2002 | last post by:
Hi From VB.NET I want to simulate the POST request of the following HTML form <html> <title>HTTP Post Testing</title> <body> <form action=http://www.example.com/postdata ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.