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]]);
        }
    }
}

 

上一篇:Flink运行时之流处理程序生成流图


下一篇:DataXceiver error processing unknown operation src: /127.0.0.1:36479 dst: /127.0.0.1:50010处理