473,378 Members | 1,631 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,378 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 3614
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;...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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:
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...
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...

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.