问题描述
- httpwebrequest网页抓取数据乱码
-
protected void Page_Load(object sender, EventArgs e)
{
string url = "http://www.veryzhun.com/planenumber.html";
string strall = HttpGet(url);
//string srr1 = GetHtmlFromGet(url,Encoding.GetEncoding("gb2312"));
}public static string HttpGet(string url) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "GET"; request.ContentType = "text/html; charset=gb2312"; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream myResponseStream = response.GetResponseStream(); StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("gb2312")); string retString = myStreamReader.ReadToEnd(); myStreamReader.Close(); myResponseStream.Close(); return retString; } 我是新手 不知道为什么还是乱码 希望给位指点
解决方案
request.ContentType = "text/html; charset=gb2312";
你设置这个没用,要看服务器返回的是什么编码的。如果服务器没有指定,那么你各种编码都试下。
时间: 2024-10-03 18:29:28