您现在的位置是:网站首页>文章详情文章详情

HttpURLConnection处理ZIP压缩的数据

inlike2022-03-04 原创文章 浏览(1508) 评论(0) 喜欢(15)

简介使用java.net.HttpURLConnection进行网络请求的时候,需要手动处理带ZIP压缩传送的数据。

使用java内置的java.util.zip.GZIPInputStream类来处理压缩数据,可以先从连接中获取传输格式:


import java.net.HttpURLConnection;
import java.net.URL;
import java.util.*;
import java.lang.Exception;
import java.io.BufferedReader;
import java.util.zip.GZIPInputStream;
import java.io.InputStreamReader;
import java.util.Map;
import org.json.JSONException;
import org.json.JSONObject;


public class test {

    public static String get(String url, Map<String, String> headers) throws JSONException {
        StringBuilder result = new StringBuilder();
        BufferedReader in = null;
        JSONObject response = new JSONObject();
        int status;
        try {
            URL realUrl = new URL(url);
            // 打开和URL之间的连接
            HttpURLConnection connection = (HttpURLConnection) realUrl.openConnection();
            // 设置通用的请求属性
            Set<String> keys = headers.keySet(); //此行可省略,直接将map.keySet()写在for-each循环的条件中

            for (String key : keys) {
                if (key != "Accept-Encoding") {
                    System.out.println("key值:" + key + " value值:" + headers.get(key));
                    connection.setRequestProperty(key, headers.get(key));
                }
            }
            // 超时时间
            connection.setConnectTimeout(2 * 1000);
            // 建立实际的连接
            connection.connect();
            status = connection.getResponseCode();
            response.put("status", Integer.toString(status));
            Map<String, List<String>> response_headers = connection.getHeaderFields();
            keys = response_headers.keySet(); //此行可省略,直接将map.keySet()写在for-each循环的条件中
            JSONObject headers_json = new JSONObject();
            for (String key : keys) {
                if (key != null) {
                    headers_json.put(key, response_headers.get(key));
                }
            }
            response.put("headers", headers_json);
            URL response_url = connection.getURL();
            response.put("url", response_url.toString());
            for (String key : response_headers.keySet()) {
                System.out.println(key + ": " + response_headers.get(key));
            }
            String encoding = connection.getContentEncoding();
            if ("gzip".equals(encoding)) {
                GZIPInputStream gZIPInputStream = new GZIPInputStream(connection.getInputStream());
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(gZIPInputStream));
                String line;
                while ((line = bufferedReader.readLine()) != null) {
                    //转化为UTF-8的编码格式
                    line = new String(line.getBytes("UTF-8"));
                    result.append(line);
                }
                bufferedReader.close();
            } else {
                // 定义 BufferedReader输入流来读取URL的响应
                in = new BufferedReader(new InputStreamReader(
                        connection.getInputStream()));
                String line;
                while ((line = in.readLine()) != null) {
                    result.append(line);
                }
            }
        } catch (Exception e) {
            System.out.println("发送GET请求出现异常!" + e);
            e.printStackTrace();
        }
        // 使用finally块来关闭输入流
        finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
                status = 503;
            }
        }
        response.put("content", result.toString());
        return response.toString();
    }
}

src=http%3A%2F%2Fimg.mp.itc.cn%2Fupload%2F20170328%2Ffa9e1f794d0d4b0aa55fbd8800fd9aff_th.jpg&refer=http%3A%2F%2Fimg.mp.itc.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1648954320&t=41116cac07f0bffe78aff1d66350d0d2


很赞哦! ( 15)
    《Python实战进阶》
    None
    None
    夏至已深

站点信息

  • 建站时间:2019-5-24
  • 网站程序:like in love
  • 主题模板《今夕何夕》
  • 文章统计:104条
  • 文章评论:***条
  • 微信公众号:扫描二维码,关注我们
  • 个人微信公众号