How to read compressed HTML page with Content-Encoding : gzip

前端 未结 3 1963
心在旅途
心在旅途 2021-01-05 04:56

I request a web page that sends a Content-Encoding: gzip header, but got stuck how to read it..

My code:

    try {
        URLConnec         


        
3条回答
  •  不思量自难忘°
    2021-01-05 05:38

    Actually, this is pb2q's answer, but I post the full code for future readers

    try {
        URLConnection connection = new URL("http://jquery.org").openConnection();                        
        String html = "";
        BufferedReader in = null;
        connection.setReadTimeout(10000);
        //The changed part
        if (connection.getHeaderField("Content-Encoding")!=null && connection.getHeaderField("Content-Encoding").equals("gzip")){
            in = new BufferedReader(new InputStreamReader(new GZIPInputStream(connection.getInputStream())));            
        } else {
            in = new BufferedReader(new InputStreamReader(connection.getInputStream()));            
        }     
        //End        
        String inputLine;
        while ((inputLine = in.readLine()) != null){
        html+=inputLine+"\n";
        }
    in.close();
        System.out.println(html);
        System.exit(0);
    } catch (IOException ex) {
        Logger.getLogger(Crawler.class.getName()).log(Level.SEVERE, null, ex);
    }
    

自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题