Max
Max

Reputation: 5063

In .NET when I get a response stream from the server what type of encoding type should I use?

In the following example I m getting a response from the server; however do I need to set ASCII or UTF8 encoding type ?

Dim objURI As Uri = New Uri(URL)
Dim wReq As WebRequest = WebRequest.Create(objURI)
Dim wResp As WebResponse = wReq.GetResponse()
Dim respStream As Stream = wResp.GetResponseStream()
Dim reader As StreamReader = New StreamReader(respStream, Encoding.ASCII)
Dim respHTML As String = reader.ReadToEnd()
wResp.Close()

Upvotes: 1

Views: 659

Answers (2)

KeithS
KeithS

Reputation: 71573

It depends on what you're expecting from the server. In the case of HTML (which seems to be your expectation), ASCII works 99% of the time for English-language pages, and should allow you to decode all HTML elements regardless. However, one of the first things you should do is read any "meta" tags in the head element of the document; these will specify the encoding of text in the body. Usually, if that isn't ASCII it's UTF-8, which is backwards-compatible to ASCII (but will show some weird characters for 2-byte and 4-byte character codes).

Upvotes: 1

Szymon Rozga
Szymon Rozga

Reputation: 18178

This really depends on what encoding the web server is sending the response in.

The docs state:

The character encoding is set by the encoding parameter, and the buffer size is set to 1024 bytes. The StreamReader object attempts to detect the encoding by looking at the first three bytes of the stream. It automatically recognizes UTF-8, little-endian Unicode, and big-endian Unicode text if the file starts with the appropriate byte order marks.

Seems to me like you should be safe just letting the runtime recognize the encoding.

Upvotes: 1

Related Questions