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();
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[];
int l = g.Read(d, , );
while (l > )
{
s.Append(Encoding.Default.GetString(d, , l));
l = g.Read(d, , );
}
Console.Write(s.ToString() + "/n/n/n" + s.Length); } private static void getHtml2(string url)
{
StringBuilder s = new StringBuilder();
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[];
int l = g.Read(d, , );
while (l > )
{
s.Append(Encoding.Default.GetString(d, , l));
l = g.Read(d, , );
}
Console.Write(s.ToString() + "/n/n/n" + s.Length);
} private static void head(HttpWebResponse r)
{
string[] keys = r.Headers.AllKeys;
for (int i = ; i < keys.Length; ++i)
{
Console.WriteLine(keys[i] + " " + r.Headers[keys[i]]);
}
}
}
C#使用Gzip解压缩完整读取网页内容的更多相关文章
- Windows API方式直接调用C#的DLL,支持多音字转拼音、Gzip解压缩、公式计算(VBA、C++、VB、Delphi甚至java都可以)
原始链接 https://www.cnblogs.com/Charltsing/p/DllExport.html 这两年,我在VBA应用方面一直有几大痛点:1.多音字转拼音:2.64位下的GZIP解压 ...
- excel保存为制表符分隔的文本文件 js无法完整读取
excel保存为制表符分隔的文本文件 js无法完整读取 excel另存为文本有两个选项,一个是制表符分隔的文本文件,一个是unicode文本.生成的文件Unicode更大一些.但是这里需要注意的是[制 ...
- linux串口通信 接收信息不完整 读取不全
类似这种 ready.o是我用来读取串口信息的一个程序 执行结果如下: [root@localhost testPlc]# ./ready.o 0 02 1 30 2 30 3 46 4 46 5 3 ...
- c# gzip解压缩
, bytes.Length)) > ) { line = System.Text.Encoding.Defaul ...
- VB6之借助zlib实现gzip解压缩
这是个简版的,可以拿来做下网页gzip的解压缩,整好我的webserver还不支持这个,有时间了就加上. zlib.dll下载请点击我! 模块zlib.bas的代码如下: 'code by lichm ...
- http gzip 解压缩
var sContentEncoding = httpRespone.Headers["Content-Encoding"]; if(sContentEncoding == &qu ...
- c语言使用zlib实现文本字符的gzip压缩与gzip解压缩
网络上找到的好多方法在解压缩字符串的时候会丢失字符,这里是解决方法: http://stackoverflow.com/questions/21186535/compressing-decompres ...
- AXIS2调用web service,返回结果用GZIP解压缩
import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOExceptio ...
- 不能完整读取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 ...
随机推荐
- 静态变量数组实现LRU算法
LRU算法的解释详情请见 https://baike.baidu.com/item/LRU/1269842 这里百度百科给出的比较详细,然后后面有一个例子 说 LRU(least recently u ...
- system times on machines may be out of sync
今天在hadoop集群执行任务的时候报了一个这个错误,听名字应该是三台机器的时间不同步.于是同步一下时间即可解决 1.安装ntpdate工具 yum -y install ntp ntpdate 2. ...
- synchronized同步语句块
用关键字synchronized声明方法在某些情况下是有弊端的,比如A线程调用同步方法执行一个长时间的任务,那么B线程则必须等待比较长时间.在这样的情况下可以使用synchronized同步语句块来解 ...
- LeetCode——Binary Tree Paths
Description: Given a binary tree, return all root-to-leaf paths. For example, given the following bi ...
- MongoDb Mmap引擎分析
版权声明:本文由孔德雨原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/137 来源:腾云阁 https://www.qclo ...
- 关于Android图片资源瘦身的奇思妙想
版权声明:本文由况鹰原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/77 来源:腾云阁 https://www.qcloud ...
- 1.border-image
1.设置在元素围绕的border的图片,用图片代替边框 语法: broder-image-source:图片 border-image-slice:切下的区域,数字|百分比(相对于图像的高度和宽度) ...
- 【Enterprise Architect 】
[Enterprise Architect ]Enterprise Architect 8 key {67SC0O95-SZPS-LIG2-YQ8Q-8D2N-KWTD-0W6R-TWDD-KT6RB ...
- INSERT高级应用
INSERT INTO departments VALUES (departments_seq.nextval, 'Entertainment', 162, 1400); INSERT INTO em ...
- C++ list容器系列功能函数详解
C++ list函数详解 首先说下eclipse工具下怎样debug:方法:你先要设置好断点,然后以Debug方式启动你的应用程序,不要用run的方式,当程序运行到你的断点位置时就会停住,也会提示你进 ...