zoukankan      html  css  js  c++  java
  • httpclient解析gzip网页

    httpclient解析gzip网页 - 夜隼 - 博客园

    请求时加上:getHC.setRequestHeader("Accept-Encoding","gzip, deflate");

    解析时

    String acceptEncoding = "";

         if(getHC.getResponseHeader("Content-Encoding")!=null)

          acceptEncoding = getHC.getResponseHeader("Content-Encoding").getValue();

         StringBuffer sb =new StringBuffer();

         log.debug("acceptEncoding:"+acceptEncoding);

         if(acceptEncoding.toLowerCase().indexOf("gzip") > -1)

         {

          //建立gzip解压工作流

          InputStream is = getHC.getResponseBodyAsStream();

          GZIPInputStream gzin = new GZIPInputStream(is);

          InputStreamReader isr = new InputStreamReader(gzin, charset); // 设置读取流的编码格式,自定义编码

          java.io.BufferedReader br = new java.io.BufferedReader(isr);

          String tempbf;

          while((tempbf=br.readLine())!=null){

           sb.append(tempbf);

           sb.append("\r\n");

          }

          isr.close();

          gzin.close();

         }

         else

         {

          InputStreamReader isr = new InputStreamReader(getHC.getResponseBodyAsStream(), charset); // 设置读取流的编码格式,自定义编码

          java.io.BufferedReader br = new java.io.BufferedReader(isr);

          String tempbf;

          while((tempbf=br.readLine())!=null){

           sb.append(tempbf);

           sb.append("\r\n");

          }

          isr.close();

         }

         getHC.abort();

         getHC.releaseConnection();

         ((SimpleHttpConnectionManager)httpClient.getHttpConnectionManager()).shutdown();

  • 相关阅读:
    谈谈Nginx有哪些特点
    网站嵌入百度地图制作
    8张图理解Java
    linux问题-easy_install安装bpython时报错
    linux问题-Centos 安装Sublime text 3
    python例子-Nmap扫描IP并更新
    python例子-PyQuery抓取信息.
    python例子-MySQLdb和练习题
    python例子-线程和队列
    mysql问题-centos7中mysql远程连接问题
  • 原文地址:https://www.cnblogs.com/lexus/p/2376812.html
Copyright © 2011-2022 走看看