C#使用Gzip解压缩完整读取网页内容
2013-08-08 10:51 狼人:-) 阅读(1186) 评论(0) 编辑 收藏 举报using System; using System.Threading; using System.Text; using System.Text.RegularExpressions; using System.IO.Compression; using System.IO; using System.Web; using System.Net; class Test { static void Main() { string url = "http://www.cnblogs.com/waw/"; getHtml1(url); getHtml2(url); Console.ReadKey(); } private static void getHtml1(string url) { StringBuilder s = new StringBuilder(102400); WebClient wr = new WebClient(); wr.Headers[HttpRequestHeader.AcceptEncoding] = "gzip, deflate"; byte[] buffer = wr.DownloadData(url); GZipStream g = new GZipStream((Stream)(new MemoryStream(buffer)), CompressionMode.Decompress); byte[] d = new byte[20480]; int l = g.Read(d, 0, 20480); while (l > 0) { s.Append(Encoding.Default.GetString(d, 0, l)); l = g.Read(d, 0, 20480); } Console.Write(s.ToString() + "/n/n/n" + s.Length); } private static void getHtml2(string url) { StringBuilder s = new StringBuilder(102400); HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url); wr.Headers[HttpRequestHeader.AcceptEncoding] = "gzip, deflate"; HttpWebResponse response = (HttpWebResponse)wr.GetResponse(); head(response); GZipStream g = new GZipStream(response.GetResponseStream(), CompressionMode.Decompress); byte[] d = new byte[20480]; int l = g.Read(d, 0, 20480); while (l > 0) { s.Append(Encoding.Default.GetString(d, 0, l)); l = g.Read(d, 0, 20480); } Console.Write(s.ToString() + "/n/n/n" + s.Length); } private static void head(HttpWebResponse r) { string[] keys = r.Headers.AllKeys; for (int i = 0; i < keys.Length; ++i) { Console.WriteLine(keys[i] + " " + r.Headers[keys[i]]); } } }
声明:此博有部分内容为转载,版权归原作者所有~