Luouy~羽林
学问如逆水行舟,不进则退; 有知识的人不实践,等于一只蜜蜂不酿蜜; 我们可以由读书而收集知识,但必须利用思考把糠和谷子分开
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://bbs.dahe.cn/bbs/thread-1647174-2498-1.html";
        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]]);
        }
    }
}
posted on 2011-07-05 17:17  羽林.Luouy  阅读(529)  评论(1编辑  收藏  举报