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


    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]]);
            }
        }
    }
  • 相关阅读:
    Educational Codeforces Round 58 A,B,C,D,E,G
    Codeforces Round #530 (Div. 2) F (树形dp+线段树)
    Codeforces Round #530 (Div. 2) A,B,C,D
    bzoj 3262: 陌上花开
    bzoj 2653: middle (主席树+二分)
    费马小定理的证明
    分组背包
    二维费用的背包问题
    luoguP1164 小A点菜(背包问题)
    完全背包问题
  • 原文地址:https://www.cnblogs.com/waw/p/3245284.html
Copyright © 2020-2023  润新知