聪明出于勤奋,天才在于积累

  :: 首页 :: 博问 :: 闪存 :: 新随笔 :: :: :: 管理 ::

从HTTP的角度

1.  客户端 在http Request  Header上带上 Accept-Encoding:gzip,deflate

2.  服务器若是支持gzip压缩则在http reponse header部分返回Content-Encoding: gzip 或者Content-Type: application/x-gzip

3.  body部分用gzip解压缩 则得到网页内容.

gzip是一种数据格式 默认且目前仅使用deflate算法压缩data部分

zlib也是一种数据格式,使用defalte算法压缩数据部分.

deflate是一种压缩算法,是huffman编码的一种加强

  

 
Accept-Enoding: gzip,deflate              表示浏览器支持gzip

Content-Encoding: gzip                     表示所请求的页面是通过gzip压缩传输的,服务器开启了gzip是一码事,在程序中使没使用是另外一回事。

 

connection = getHttpConnection(urlString);
connection.addRequestProperty("Accept-Encoding", "gzip,deflate");
bufReader = new BufferedReader(new InputStreamReader( new GZIPInputStream(connection.getInputStream()), "GBK"));
totalBytes = connection.getContentLength();
String tmpLine = null;
while ((tmpLine = bufReader.readLine()) != null) {
bufStr.append(tmpLine);
}

 

即使服务器开启了gzip,如果在请求的头里面不加 ("Accept-Encoding", "gzip,deflate"),获取的流还是不压缩的。


 

 

posted on 2011-12-07 17:40    阅读(480)  评论(0编辑  收藏  举报