/// <summary> /// 获取HttpWebResponse内流的编码格式并返回内含的流 /// </summary> /// <param name="myResponse">Internet资源的响应</param> /// <param name="stream">返回流</param> /// <returns>流的编码</returns> public Encoding Detector(HttpWebResponse response, out Stream stream) { var encoding = Detect(response.CharacterSet); if (encoding == null) { encoding = Detect(RegexUtility.GetFirstMatch(reg, response.ContentType)); } if (encoding != null) { stream = response.GetResponseStream(); return(encoding); } return(ByHtmlCharset(response.GetResponseStream(), out stream)); }
/// <summary> /// 根据页面内容获取流的编码形式,如果没有则默认为系统编码 /// </summary> /// <param name="memoryStream">内存流</param> private Encoding ByHtmlCharset(Stream rs, out Stream ms) { ms = rs.ToMemoryStream(); var streamReader = new StreamReader(ms, Encoding.ASCII); var html = streamReader.ReadToEnd(); var encode = DefaultEncoding; if (reg.IsMatch(html)) { encode = ConvertEncoding(RegexUtility.GetFirstMatch(reg, html)); } ms.Seek(0, SeekOrigin.Begin); return(encode); }