C#使用GZIP解压缩完整读取网页内容

网友投稿 250 2022-11-21

C#使用GZIP解压缩完整读取网页内容

///

/// C#使用GZIP解压缩完整读取网页内容 /// /// /// public static string GetHtmlWithUtf(string url) { if (!(url.Contains("|| url.Contains("https://"))) { url = "+ url; } HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url); req.UserAgent = "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)"; req.Accept = "*/*"; req.Headers.Add("Accept-Language", "zh-cn,en-us;q=0.5"); req.ContentType = "text/xml"; string sHTML = string.Empty; using (HttpWebResponse response = (HttpWebResponse)req.GetResponse()) { if (response.ContentEncoding.ToLower().Contains("gzip")) { using (GZipStream stream = new GZipStream(response.GetResponseStream(), CompressionMode.Decompress)) { using (StreamReader reader = new StreamReader(stream, Encoding.UTF8)) { sHTML = reader.ReadToEnd(); } } } else if (response.ContentEncoding.ToLower().Contains("deflate")) { using (DeflateStream stream = new DeflateStream(response.GetResponseStream(), CompressionMode.Decompress)) { using (StreamReader reader = new StreamReader(stream, Encoding.UTF8)) { sHTML = reader.ReadToEnd(); } } } else { using (Stream stream = response.GetResponseStream()) { using (StreamReader reader = new StreamReader(stream, Encoding.UTF8)) { sHTML = reader.ReadToEnd(); } } } } return sHTML; }

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:使用JPA自定义VO接收返回结果集(unwrap)
下一篇:TLC5620I与TMS320F2812的接口设计
相关文章

 发表评论

暂时没有评论,来抢沙发吧~