JAVA HttpUrlConnection 使用 GZIP 编码压缩

By default, when you make a request to URL, the response is not compressed.

默认情况下,当你在URL创建一个request 请求,response响应是不压缩的。

比如,代码:

java 复制代码
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpConnect {
    public static void main(String[] args) throws Exception {
      URL url = new URL("http://www.rgagnon.com/howto.html");
      HttpURLConnection con = (HttpURLConnection) url.openConnection();
      // con.setRequestProperty("Accept-Encoding", "gzip");
      System.out.println("Length : " + con.getContentLength());
      Reader reader = new InputStreamReader(con.getInputStream());
      while (true) {
        int ch = reader.read();
        if (ch==-1) {
          break;
        }
        System.out.print((char)ch);
      }
    }
}

From the response, we see that the length is 21740 bytes.

从响应内容,我们看到长度是21740字节。

html 复制代码
Length : 21740
<!DOCTYPE HTML>
<HTML>
    <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <LINK REL="SHORTCUT ICON" HREF="http://www.rgagnon.com/favicon.ico">
      <META NAME="description"
            Content="Real's JAVA JAVASCRIPT WSH and PowerBuilder How-to pages with useful code snippets">
      <META NAME="keywords"
      Content="java,javascript,wsh,vbscript,how-to,powerbuilder">
...

By setting the Request Header "Accept" to "gzip", we are telling to the server that we want the response to be compressed if possible with the GZIP compression scheme. If it's not supported by the server then the response will be sent as plain text.

通过设置request header 的属性"Accept" 为"gzip",我们告诉服务器在可能Gzip压缩的情况下我们想要服务器压缩响应。如果服务器不支持压缩应当以纯文本发送响应。

java 复制代码
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.HttpURLConnection;
import java.net.URL;


public class HttpConnect {
    public static void main(String[] args) throws Exception {
      URL url = new URL("http://www.rgagnon.com/howto.html");
      HttpURLConnection con = (HttpURLConnection) url.openConnection();
      con.setRequestProperty("Accept-Encoding", "gzip");
      System.out.println("Length : " + con.getContentLength());
      Reader reader = new InputStreamReader(con.getInputStream());
      while (true) {
        int ch = reader.read();
        if (ch==-1) {
          break;
        }
        System.out.print((char)ch);
      }
    }
}

Now the response length is 4865 but the content is not readable because we need to uncompress it!

现在响应长度是4865,但是内容不可读因为我们需要jie压缩!

相关推荐
ChinaRainbowSea9 分钟前
1. 初始 RabbitMQ 消息队列
java·中间件·rabbitmq·java-rabbitmq
lmryBC4918 分钟前
golang接口-interface
java·前端·golang
ゞ 正在缓冲99%…19 分钟前
leetcode75.颜色分类
java·数据结构·算法·排序
孤独得猿20 分钟前
Qt常用控件第一部分
服务器·开发语言·qt
慕斯策划一场流浪25 分钟前
fastGPT—nextjs—mongoose—团队管理之团队列表api接口实现
开发语言·前端·javascript·fastgpt env文件配置·fastgpt团队列表接口实现·fastgpt团队切换api·fastgpt团队切换逻辑
橘猫云计算机设计31 分钟前
基于springboot的考研成绩查询系统(源码+lw+部署文档+讲解),源码可白嫖!
java·spring boot·后端·python·考研·django·毕业设计
时光呢36 分钟前
JAVA常见的 JVM 参数及其典型默认值
java·开发语言·jvm
橙橙子23037 分钟前
c++柔性数组、友元、类模版
开发语言·c++·柔性数组
程序媛学姐44 分钟前
SpringKafka错误处理:重试机制与死信队列
java·开发语言·spring·kafka
2401_840192271 小时前
如何学习一门计算机技术
开发语言·git·python·devops