473,386 Members | 1,715 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,386 software developers and data experts.

Incorrect HttpWebResponse.CharacterSet

My Page contains this:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
i used this code to retrieve the page:

HttpWebResponse webresp = (HttpWebResponse)(webrq.GetResponse());

the value of the some properties of webreps are as follows:

ContentType = "text/html"
ContentEncoding = ""
CharacterSet = "ISO-8859-1"

can any1 say why CharacterSet is ISO instead of utf-8?
how can i fix this?

Info:
the page is served by IIS (on WinXp SP2)
IE shows the page correctly but

textBoxHTMLSource.Text =
Encoding.GetEncoding(webresp.CharacterSet).GetStri ng(data, 0, len);

the textbox shows characters incorrectly (data is recieved through
webresp.GetStream())

Sep 23 '06 #1
6 6978


Leon_Amirreza wrote:
My Page contains this:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
i used this code to retrieve the page:

HttpWebResponse webresp = (HttpWebResponse)(webrq.GetResponse());

the value of the some properties of webreps are as follows:

ContentType = "text/html"
ContentEncoding = ""
CharacterSet = "ISO-8859-1"
Is that a public URL? Then post it so that we can simply check the HTTP
headers (as any HTTP Content-Type response header is more important than
that meta element, in particular for the .NET HttpWebResponse which will
hardly have any code to look at HTML meta elements. A browser might look
at the meta element, but not a generic HTTP API like .NET's
HttpWebRequest/Response).


--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Sep 23 '06 #2
no unfortunately its not a public url actually its on my winxp.
I used the debugger to list the

HttpWebResponse.Headers

It is as follows:

{Accept-Ranges: bytes
Content-Length: 8609
Content-Type: text/html
Date: Sun, 24 Sep 2006 17:32:55 GMT
ETag: "3a51249e792c61:900"
Last-Modified: Sun, 18 Jun 2006 14:55:48 GMT
Server: Microsoft-IIS/5.1
X-Powered-By: ASP.NET

}

"Martin Honnen" <ma*******@yahoo.dewrote in message
news:OK**************@TK2MSFTNGP05.phx.gbl...
>

Leon_Amirreza wrote:
>My Page contains this:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
i used this code to retrieve the page:

HttpWebResponse webresp =
(HttpWebResponse)(webrq.GetResponse());

the value of the some properties of webreps are as follows:

ContentType = "text/html"
ContentEncoding = ""
CharacterSet = "ISO-8859-1"

Is that a public URL? Then post it so that we can simply check the HTTP
headers (as any HTTP Content-Type response header is more important than
that meta element, in particular for the .NET HttpWebResponse which will
hardly have any code to look at HTML meta elements. A browser might look
at the meta element, but not a generic HTTP API like .NET's
HttpWebRequest/Response).


--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/

Sep 23 '06 #3


Leon_Amirreza wrote:
I used the debugger to list the

HttpWebResponse.Headers

It is as follows:

{Accept-Ranges: bytes
Content-Length: 8609
Content-Type: text/html
Date: Sun, 24 Sep 2006 17:32:55 GMT
ETag: "3a51249e792c61:900"
Last-Modified: Sun, 18 Jun 2006 14:55:48 GMT
Server: Microsoft-IIS/5.1
X-Powered-By: ASP.NET
That means the server does not tell anything about the encoding/charset
of the response. I am not sure whether that CharacterSet property takes
on some default, the documentation does not tell:
<http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemNetHttpWebResponseClassCharacterSetTopi c.asp>

As said, the HttpWebResponse does probably not have any support for
checking the meta element of a HTML document it receives. If that
text/html is on your server then you might want to have the server send
a charset parameter for the Content-Type header.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Sep 24 '06 #4
i used an ASP.NET line of code to have server send charset in ContentType
but according to IIS Documentation these two lines of code are equivalent:

<META HTTP-EQUIV="Content-Type" CONTENT="text/html; CHARSET=windows-1251">
<% Response.Charset = "windows-1252" %>Title : Setting the Code Page for
String Conversionsurl:
http://localhost/iishelp/iis/htm/asp/eadg6e7n.htmBut using the meta tag does
not seem to send charset in the ContentType of HTTP Header!Is this a problem
in my Web Server configuration or these 2 line of code are not actually the
same?!"Martin Honnen" <ma*******@yahoo.dewrote in message
news:Oy**************@TK2MSFTNGP06.phx.gbl...
>

Leon_Amirreza wrote:
>I used the debugger to list the

HttpWebResponse.Headers

It is as follows:

{Accept-Ranges: bytes
Content-Length: 8609
Content-Type: text/html
Date: Sun, 24 Sep 2006 17:32:55 GMT
ETag: "3a51249e792c61:900"
Last-Modified: Sun, 18 Jun 2006 14:55:48 GMT
Server: Microsoft-IIS/5.1
X-Powered-By: ASP.NET

That means the server does not tell anything about the encoding/charset of
the response. I am not sure whether that CharacterSet property takes on
some default, the documentation does not tell:
<http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemNetHttpWebResponseClassCharacterSetTopi c.asp>

As said, the HttpWebResponse does probably not have any support for
checking the meta element of a HTML document it receives. If that
text/html is on your server then you might want to have the server send a
charset parameter for the Content-Type header.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/

Sep 24 '06 #5
i used an ASP.NET line of code to have server send charset in ContentType
but according to IIS Documentation these two lines of code are equivalent:

<META HTTP-EQUIV="Content-Type" CONTENT="text/html; CHARSET=windows-1251">

<% Response.Charset = "windows-1252" %>

Title : Setting the Code Page for String Conversions

url: http://localhost/iishelp/iis/htm/asp/eadg6e7n.htm

But using the meta tag does not seem to send charset in the ContentType of
HTTP Header!
Is this a problem in my Web Server configuration or these 2 line of code are
not actually the
same?!
"Martin Honnen" <ma*******@yahoo.dewrote in message
news:Oy**************@TK2MSFTNGP06.phx.gbl...
>

Leon_Amirreza wrote:
>I used the debugger to list the

HttpWebResponse.Headers

It is as follows:

{Accept-Ranges: bytes
Content-Length: 8609
Content-Type: text/html
Date: Sun, 24 Sep 2006 17:32:55 GMT
ETag: "3a51249e792c61:900"
Last-Modified: Sun, 18 Jun 2006 14:55:48 GMT
Server: Microsoft-IIS/5.1
X-Powered-By: ASP.NET

That means the server does not tell anything about the encoding/charset of
the response. I am not sure whether that CharacterSet property takes on
some default, the documentation does not tell:
<http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemNetHttpWebResponseClassCharacterSetTopi c.asp>

As said, the HttpWebResponse does probably not have any support for
checking the meta element of a HTML document it receives. If that
text/html is on your server then you might want to have the server send a
charset parameter for the Content-Type header.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/

Sep 24 '06 #6
Thus wrote Leon_Amirreza,
i used an ASP.NET line of code to have server send charset in
ContentType but according to IIS Documentation these two lines of code
are equivalent:

<META HTTP-EQUIV="Content-Type" CONTENT="text/html;
CHARSET=windows-1251">

<% Response.Charset = "windows-1252" %>
That's plain wrong. First of all, Windows-1251 and Windows-1252 are different
encodings (that's a silly error in the IIS docs).

But more importantly, HTTP doesn't care about any HTML content, so specifying
a META tag works for clients that parse HTML (i.e. web browsers), but not
for HTTP intermediaries that process only HTTP headers. Setting a META tag
has no effect on HTTP headers in ASP.NET.

Next, all but forget about using the HttpResponse.Charset property -- that's
ASP programming.

The proper way of specifying a character encoding in ASP.NET is to either
1) use the default response encoding specified in your web.config's <globalization
/element, UTF-8 by default
2) declare a ResponseEncoding in your page directive: <%@ Page Language="C#"
ResponseEncoding="ISO-8859-1" %>
3) declare a CodePage in your page directive: <%@ Page Language="C#" CodePage="28591"
%>
4) programmatically set the HttpResponse.ContentEncoding property: Response.ContentEncoding
= Encoding.GetEncoding(28591);

The only difference between 2) and 3) is that 2) relies on character encoding
(i.e. IANA) names, whereas 3) uses numeric Win32 codepage identifiers as
per http://msdn.microsoft.com/library/de...icode_81rn.asp

All of the above implicitly set the HttpResponse.Charset to its appropriate
value (with one caveat -- see below). Vice versa, the same is *not* true.
Setting HttpResponse.Charset has no effect on the actual encoding applied
to the output stream. It's just a string that ends up in the Content-Type
header. Thus, you should never set Charset in your code (but see below**).

Morale of the story: ASP.NET sets the appropriate Content-Type header that
includes a charset attribute, unless you break stuff ;-)

For static content (i.e. that is directly served by IIS) none of the above
applies. See http://www.w3.org/International/O-HTTP-charset for how to set
the appropriate Content-Type in these case.

**
When to set Charset: There's one esoteric case I'm aware of when you *do*
want to set HttpResponse.Charset in your code, and that's when you're using
UTF-16BE as response encoding. In this case, ASP.NET uses an invalid IANA
name, which is not recognized by all browsers. It sets "unicodeFFFE" instead
of "utf-16be".

Cheers,
--
Joerg Jooss
ne********@joergjooss.de
Sep 25 '06 #7

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

Similar topics

4
by: CJ Oxx | last post by:
I have a problem with browser charset recognition when using PHP 4.1.2 (this is the PHP version which our hosting company provides). For charset recognition, I use the following meta-tag: <meta...
0
by: Denny Rue | last post by:
I’ve created VB code to download files from a web site through the use of HTTPWebRequest, HTTPWebResponse and BinaryReader. The HTTPWebRequest has a TimeOut property to limit how long it waits...
0
by: hlabbott | last post by:
Hi I'm having a problem displaying attachments correctly. I have messages with attachments stored on Exchange2000 and want to be able to click a hyperlink in my project like in OWA and see an...
3
by: Jaroslav Jakes | last post by:
Hi, I can't find out, how to detect the characterset of a text file. Orders I receive by mail have the same structure, but are created on different systems, like DOS, Windows, Linux. So the...
13
by: Jason Manfield | last post by:
For some URLs (e.g.http://v3.espacenet.com/origdoc?DB=EPODOC&IDX=WO2005028634&F=0&QPN=WO2005028634), the content length for the HttpWebResponse I get with request.GetResponse in empty. The...
1
by: Jason Manfield | last post by:
Why does HttpWebResponse.CharacterSet always return ISO-8859-1? I am accessing a Chinese Web site (http://cn.yahoo.com) -- View Source in IE shows the character set to be "gb2312", but the...
3
by: Nuno Magalhaes | last post by:
Hello, In a simple thread I have a code like the one below: public void ProtectionRun() { while(true) { //Sleep thread for one minute //Thread.Sleep(60000); HttpWebRequest
2
by: Noggin The Nog | last post by:
Hi all, I've been trying to get HttpWebResponse to work, but whenever I try I get "The operation has timed-out". I'm simply trying to send an HTTP request querystring to a remote website and read...
2
by: Nuno Magalhaes | last post by:
In pages that there is no content length, how does HttpWebResponse knows where the page ends? And what kind of objects/methods does it retrieve? Does it only retrieve the initial page without any...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.