我看到了很多例子,但是它们都将它们读入字节数组或者256个字符,慢慢地读入。为什么?
将结果 Stream值转换为一个字符串以便我能够解析它,这样做是否明智?
Stream
You should create a StreamReader around the stream, then call ReadToEnd.
StreamReader
ReadToEnd
You should consider calling WebClient.DownloadString instead.
WebClient.DownloadString
You can use StreamReader.ReadToEnd(),
StreamReader.ReadToEnd()
using (Stream stream = response.GetResponseStream()) { StreamReader reader = new StreamReader(stream, Encoding.UTF8); String responseString = reader.ReadToEnd(); }
You can create a StreamReader around the stream, then call StreamReader.ReadToEnd().
StreamReader responseReader = new StreamReader(request.GetResponse().GetResponseStream()); var responseData = responseReader.ReadToEnd();
As @Heinzi mentioned the character set of the response should be used.
var encoding = response.CharacterSet == "" ? Encoding.UTF8 : Encoding.GetEncoding(response.CharacterSet); using (var stream = response.GetResponseStream()) { var reader = new StreamReader(stream, encoding); var responseString = reader.ReadToEnd(); }
Richard Schneider is right. use code below to fetch data from site which is not utf8 charset will get wrong string.
" i can't vote.so wrote this.