StreamReader.ReadToEnd()
? (a)
Dim strWebResponse As String
Dim Request As HttpWebRequest = WebRequest.Create(Url)
Using Response As WebResponse = smsRequest.GetResponse()
Using reader As StreamReader = New StreamReader(Response.GetResponseStream())
strWebResponse = reader.ReadToEnd()
End Using
End Using
(b)
Dim encoding As New UTF8Encoding()
Dim strWebResponse As String
Dim Request As HttpWebRequest = WebRequest.Create(Url)
Using Response As WebResponse = Request.GetResponse()
Dim responseBuffer(Response.ContentLength - 1) As Byte
Response.GetResponseStream().Read(responseBuffer, 0, Response.ContentLength - 1)
strWebResponse = encoding.GetString(responseBuffer)
End Using
StreamReader is designed for character input in a particular encoding, whereas the Stream class is designed for byte input and output. Use StreamReader for reading lines of information from a standard text file.
UTF-8 (UCS Transformation Format 8) is the World Wide Web's most common character encoding. Each character is represented by one to four bytes. UTF-8 is backward-compatible with ASCII and can represent any standard Unicode character.
This constructor initializes the encoding as specified by the encoding parameter, the BaseStream property using the stream parameter, and the internal buffer size to 1024 bytes. The detectEncodingFromByteOrderMarks parameter detects the encoding by looking at the first four bytes of the stream.
The standard encoding used by StreamReader
is Encoding.Default, which will vary from machine to machine depending on your version of Windows and the locale that you have set. Encoding.UTF8
.
I have trouble remembering what the defaults are, so I prefer to use the StreamReader
constructor that lets me specify the encoding. For example:
Using reader As StreamReader = New StreamReader(Response.GetResponseStream(), Encoding.UTF8)
See the constructor documentation for more info.
If you use that constructor in your example a, the results will be the same as for your example b.
Should you use UTF-8? That depends on the page you're downloading. If the page you're downloading was encoded with UTF-8 then, yes, you should use UTF-8. UTF-8 is supposed to be the default if no character set is defined in the HTTP headers. But you need to check the Content-Type
header to determine if the page uses some other encoding. For example, the Content-Type
header might read:
application/xml; charset=ISO-8859-2
You would have to examine the ContentType property of the HttpWebResponse
, check to see if there is a charset
field, and set the encoding properly based on that.
Or, just use UTF-8 and hope for the best.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With