用stream.Read不能完整读取内容的问题。
我在网上找到C#获取网页内容的代码:http://qilinwuxian.blogchina.com/1438711.html
在获取小网页的时候没什么问题,但是网页比较大时文本会截断,用StreamReader就没有什么问题,不知道是为什么?
用代码中的5,6,7行的方法,count的值明显小于buf的长度,为什么不能读完全呢?
1
HttpWebRequest HttpWReq = (HttpWebRequest)WebRequest.Create("http://www.cnblogs.com/rss.aspx?id=-1");
2
HttpWebResponse HttpWResp = (HttpWebResponse)HttpWReq.GetResponse();
3
System.IO.Stream stream = HttpWResp.GetResponseStream();
4
5
//byte[] buf = new byte[38192];
6
//int count = stream.Read(buf,0,buf.Length);
7
//txtResult.Text = System.Text.Encoding.UTF8.GetString(buf, 0,count);
8
9
System.IO.StreamReader sr = new System.IO.StreamReader(stream,System.Text.Encoding.UTF8);
10
txtXML.Text = sr.ReadToEnd();
HttpWebRequest HttpWReq = (HttpWebRequest)WebRequest.Create("http://www.cnblogs.com/rss.aspx?id=-1");2
HttpWebResponse HttpWResp = (HttpWebResponse)HttpWReq.GetResponse();3
System.IO.Stream stream = HttpWResp.GetResponseStream();4

5
//byte[] buf = new byte[38192]; 6
//int count = stream.Read(buf,0,buf.Length);7
//txtResult.Text = System.Text.Encoding.UTF8.GetString(buf, 0,count); 8

9
System.IO.StreamReader sr = new System.IO.StreamReader(stream,System.Text.Encoding.UTF8);10
txtXML.Text = sr.ReadToEnd();
浙公网安备 33010602011771号