httpwebrequest网页抓取数据乱码

1、可能的原因是下载网页的编码和网页编码不一致

HttpWebResponse response = (HttpWebResponse)request.GetResponse();

Encoding cd = System.Text.Encoding.GetEncoding(response.CharacterSet);

Stream resStream = response.GetResponseStream();

StreamReader sr = new StreamReader(resStream, cd);

html = sr.ReadToEnd();


2、可能的原因是下载的网页压缩过了

 
  

HttpWebResponse response = (HttpWebResponse)request.GetResponse();

Encoding cd = System.Text.Encoding.GetEncoding(response.CharacterSet);

Stream resStream = response.GetResponseStream();

try {         if (response.ContentEncoding.ToLower().Contains("gzip"))          {                resStream = new GZipStream(resStream, CompressionMode.Decompress);          } } catch { }

StreamReader sr = new StreamReader(resStream, cd);

html = sr.ReadToEnd();


你可能感兴趣的:(httpwebrequest网页抓取数据乱码)