1. using System;
  2. using System.Threading;
  3. using System.Text;
  4. using System.Text.RegularExpressions;
  5. using System.IO.Compression;
  6. using System.IO;
  7. using System.Web;
  8. using System.Net;
  9.  
  10. class Test
  11. {
  12. static void Main()
  13. {
  14. string url = "http://www.cnblogs.com/waw/";
  15. getHtml1(url);
  16. getHtml2(url);
  17. Console.ReadKey();
  18. }
  19.  
  20. private static void getHtml1(string url)
  21. {
  22. StringBuilder s = new StringBuilder();
  23. WebClient wr = new WebClient();
  24. wr.Headers[HttpRequestHeader.AcceptEncoding] = "gzip, deflate";
  25. byte[] buffer = wr.DownloadData(url);
  26. GZipStream g = new GZipStream((Stream)(new MemoryStream(buffer)), CompressionMode.Decompress);
  27. byte[] d = new byte[];
  28. int l = g.Read(d, , );
  29. while (l > )
  30. {
  31. s.Append(Encoding.Default.GetString(d, , l));
  32. l = g.Read(d, , );
  33. }
  34. Console.Write(s.ToString() + "/n/n/n" + s.Length);
  35.  
  36. }
  37.  
  38. private static void getHtml2(string url)
  39. {
  40. StringBuilder s = new StringBuilder();
  41. HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url);
  42. wr.Headers[HttpRequestHeader.AcceptEncoding] = "gzip, deflate";
  43. HttpWebResponse response = (HttpWebResponse)wr.GetResponse();
  44. head(response);
  45. GZipStream g = new GZipStream(response.GetResponseStream(), CompressionMode.Decompress);
  46. byte[] d = new byte[];
  47. int l = g.Read(d, , );
  48. while (l > )
  49. {
  50. s.Append(Encoding.Default.GetString(d, , l));
  51. l = g.Read(d, , );
  52. }
  53. Console.Write(s.ToString() + "/n/n/n" + s.Length);
  54. }
  55.  
  56. private static void head(HttpWebResponse r)
  57. {
  58. string[] keys = r.Headers.AllKeys;
  59. for (int i = ; i < keys.Length; ++i)
  60. {
  61. Console.WriteLine(keys[i] + " " + r.Headers[keys[i]]);
  62. }
  63. }
  64. }

C#使用Gzip解压缩完整读取网页内容的更多相关文章

  1. Windows API方式直接调用C#的DLL,支持多音字转拼音、Gzip解压缩、公式计算(VBA、C++、VB、Delphi甚至java都可以)

    原始链接 https://www.cnblogs.com/Charltsing/p/DllExport.html 这两年,我在VBA应用方面一直有几大痛点:1.多音字转拼音:2.64位下的GZIP解压 ...

  2. excel保存为制表符分隔的文本文件 js无法完整读取

    excel保存为制表符分隔的文本文件 js无法完整读取 excel另存为文本有两个选项,一个是制表符分隔的文本文件,一个是unicode文本.生成的文件Unicode更大一些.但是这里需要注意的是[制 ...

  3. linux串口通信 接收信息不完整 读取不全

    类似这种 ready.o是我用来读取串口信息的一个程序 执行结果如下: [root@localhost testPlc]# ./ready.o 0 02 1 30 2 30 3 46 4 46 5 3 ...

  4. c# gzip解压缩

                , bytes.Length)) > )             {                line =  System.Text.Encoding.Defaul ...

  5. VB6之借助zlib实现gzip解压缩

    这是个简版的,可以拿来做下网页gzip的解压缩,整好我的webserver还不支持这个,有时间了就加上. zlib.dll下载请点击我! 模块zlib.bas的代码如下: 'code by lichm ...

  6. http gzip 解压缩

    var sContentEncoding = httpRespone.Headers["Content-Encoding"]; if(sContentEncoding == &qu ...

  7. c语言使用zlib实现文本字符的gzip压缩与gzip解压缩

    网络上找到的好多方法在解压缩字符串的时候会丢失字符,这里是解决方法: http://stackoverflow.com/questions/21186535/compressing-decompres ...

  8. AXIS2调用web service,返回结果用GZIP解压缩

    import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOExceptio ...

  9. 不能完整读取txt文件问题

    txt文件内容 5 1.3 0.4 3.4 -1.7 16.7 0.89 14.17 4.8 1.34 0.42 3.36 -2 16.2 0.9 14.8 4.9 1.30 0.37 3.51 -1 ...

随机推荐

  1. brew 接口的原理

    请查看相关文档的第9章 该文档可以csdn silentjesse帐号下的资源去下载 http://download.csdn.net/detail/silentjesse/5859077

  2. [Scikit-learn] *Dynamic Bayesian Network - Partical Filter

    涉及的一些知识: 机器人的自我定位 Sequential Importance Sampling Ref: http://scipy-cookbook.readthedocs.io/items/Par ...

  3. 第五章 面向方面编程___AOP入门

    上一篇讲了 AOP 和 OOP 的区别,这一次我们开始入门 AOP .实现面向方面编程的技术,主要分为两大类: 一是 采用动态代理技术,利用截取消息的方式,对该消息进行装饰,以取代原有对象行为的执行: ...

  4. Strut2------源码下载

    转载: http://download.csdn.net/detail/dingkui/6858009

  5. PHP-005

    MySql 表列默认时间类型设置:数据类型:timestamp,缺省值:CURRENT_TIMESTAMP

  6. 简单易懂的snmpd.conf配置文件说明

    转自http://blog.chinaunix.net/u2/61187/showart_689604.html 用 snmp+mrtg 可以很好的实现对局域网内服务器状态的监控.      现在就以 ...

  7. apache 图片防盗链

    RewriteEngine on RewriteCond %{HTTP_REFERER} !ot.com [NC] RewriteCond %{HTTP_REFERER} !baidu.com [NC ...

  8. chmod 777 修改权限

    http://william71.blogbus.com/logs/33484772.html 在Unix和Linux的各种操作系统下,每个文件(文件夹也被看作是文件)都按读.写.运行设定权限.例如我 ...

  9. hadoop错误各种原因

    NoRouteToHostException 错误描述: INFO hdfs.DFSClient: Exception in createBlockOutputStream java.net.NoRo ...

  10. AndroidのListView之滑动列表项(点击事件和滑动事件共存)

    这里正好在项目有这么一个bt的需求,如下图ListView的item可以响应点击事件也可以响应item的左右滑动事件,两个事件可以相互独立互不影响. 听说iphone的list选项就有这样bt的功能, ...