对包体内容进行gzip压缩

时间:2023-01-23 13:49:14
try{
  HttpEntity requestEntity = new ByteArrayEntity(GzipUtil.compress(JSON.toJSONString(param),"UTF-8"));

    CloseableHttpClient client = HttpClients.createDefault();
    HttpPost httpPost = new HttpPost(url);
    httpPost.addHeader("Content-type", "application/json; charset=UTF-8");
    httpPost.setHeader("Accept", "application/json");
    httpPost.setHeader("Content-Encoding", "gzip");
    httpPost.setEntity(requestEntity);
    CloseableHttpResponse response = client.execute(httpPost);
    HttpEntity responseEntity = response.getEntity();
    String body = null;
    if (responseEntity != null){
        body = EntityUtils.toString(responseEntity,"UTF-8");
    }
    EntityUtils.consume(responseEntity);
    response.close();
    return body;
}catch (Exception e){
        e.printStackTrace();
}