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

Getting correct encoding from a webresponse

Hi guys,

I have the following code that retrieves a webpage.
My problem is getting it to use the right encoding.
I've tested it against a danish page, but it won't show the danish
characters. When I set it to sniff the encoding (using
sr = New System.IO.StreamReader(strm, True)
) it sets it to UTF8, but when I browse the page using MSIE with
auto-selection of encoding, it uses Western European (and displays the danish
chars correctly).

Any ideas?

The code:
' Create a new WebRequest Object to the mentioned URL.
Dim myWebRequest As System.Net.WebRequest =
System.Net.WebRequest.Create(txtUrl.Text)

' Set the 'Timeout' property in Milliseconds.
myWebRequest.Timeout = 10000

Dim encode As System.Text.Encoding =
System.Text.Encoding.GetEncoding("Unicode")

' Assign the response object of 'WebRequest' to a 'WebResponse'
variable.
Dim myWebResponse As System.Net.WebResponse =
myWebRequest.GetResponse()
Dim strm As System.IO.Stream
Dim sr As System.IO.StreamReader
Dim line As String

strm = myWebResponse.GetResponseStream()

sr = New System.IO.StreamReader(strm, True)
'sr = New System.IO.StreamReader(strm, encode)
MsgBox("Encoding: " & sr.CurrentEncoding.ToString)
txtSrc.Text = ""
Do
line = sr.ReadLine()
txtSrc.Text += line
Loop While Not line Is Nothing
Jul 21 '05 #1
2 3618
Jonax <Jo***@discussions.microsoft.com> wrote:
I have the following code that retrieves a webpage.
My problem is getting it to use the right encoding.
I've tested it against a danish page, but it won't show the danish
characters. When I set it to sniff the encoding (using
sr = New System.IO.StreamReader(strm, True)
) it sets it to UTF8, but when I browse the page using MSIE with
auto-selection of encoding, it uses Western European (and displays the danish
chars correctly).

Any ideas?


You should look at what the HttpWebResponse says the ContentEncoding is
- although the server may not tell you. Guessing accurately is tricky
and there's always a chance you'll get it wrong. To use "Western
European" however, use Encoding.GetEncoding(1252).

(StreamReader will always use UTF-8 by default, as the documentation
states.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #2
Thx Jon,

I'll have a go at it - and merry xmas...

Jul 21 '05 #3

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

Similar topics

1
by: Thomas Rokamp | last post by:
Hi! I'm testing a russian webpage with HttpUnit, but it seems, that it doesn't support the encoding (Win-1251). How can I add support for this in Java or HttpUnit? ...
4
by: H Lee | last post by:
Hi, I'm an XML newbie, and not sure if this is the appropriate newsgroup to post my question, so feel free to suggest other newgroups where I should post this message if this is the case. I'm...
5
by: Nuno Magalhaes | last post by:
How can I get all the attributes of an HttpWebResponse? I do a request to this site: http://pipa.inov.pt/cgi-bin/camera but originaly it comes with a PAGE attribute that gives me the time in clock...
8
by: Brent White | last post by:
If you need more information, I'd be glad to give it, but I am developing an ASPX page so that the user can select multiple values for a specific field, then pass them on (using Crystal Reports 10...
4
by: Jonax | last post by:
Hi guys, I have the following code that retrieves a webpage. My problem is getting it to use the right encoding. I've tested it against a danish page, but it won't show the danish characters....
2
by: Jerry Spence1 | last post by:
I am trying to come up with a focussing routine for a camera and I need to detect when the image is in focus. As I understand it, JPG Image size goes up sharply when it is in focus as it is very...
0
by: Osamede Zhang | last post by:
I downloaded some web pages use System.Net.HttpWebResponse,and get string from it Like this: webresponse = (System.Net.HttpWebResponse)req.GetResponse(); System.IO.StreamReader stream =...
1
by: | last post by:
I have a function which takes some parameters and gets a web page which includes some specific Turkish characters. Im using UTF-8 encoding , but in my response text there are ? charecters instead...
0
by: deeps1512 | last post by:
hi All, I have written the following code to get a webresponse. XmlDocument myxmlDoc1 = new XmlDocument(); myxmlDoc1.LoadXml(BkXMLRQ); string s = myxmlDoc1.OuterXml;...
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:
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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.