从网页上读过来的html代码显示在控制台上是乱码,这个怎么解决?
下面是我读页面的代码:
- C# code
/// <summary> /// 根据ulr读出html /// </summary> static string GetHtml(string url) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream stream = response.GetResponseStream(); StreamReader sReader = new StreamReader(stream, Encoding.Default); string html = sReader.ReadToEnd().Replace("\r\n", ""); response.Close(); Console.WriteLine(html); return html; }
------解决方案--------------------------------------------------------
new StreamReader(stream, Encoding.Default);
看下请求的网页的编码
//new StreamReader(stream, Encoding.UTF8);
//new StreamReader(stream, Encoding.GetEncoding("gb2312"));
改下试试