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压缩!

相关推荐
AD钙奶-lalala6 分钟前
SpringBoot 4.0.3配置Swagger
java·spring boot·后端
小龙报8 分钟前
【算法通关指南:算法基础篇】二分算法: 1.A-B 数对 2.烦恼的高考志愿
c语言·开发语言·数据结构·c++·vscode·算法·二分
seven97_top9 分钟前
NIO:解开非阻塞I/O高并发编程的秘密
java
小六溜了9 分钟前
模块二十.双列集合
java
23.11 分钟前
【Java】NIO 中的多路复用(Selector / Channel)机制
java·面试·nio
yong999011 分钟前
NNDA、PDA、JPDA、IMM数据关联算法MATLAB实现
开发语言·算法·matlab
七夜zippoe14 分钟前
Docker容器化实战:核心概念、镜像制作与多阶段构建全解析
java·jvm·数据库·docker·oracle·容器化
TimberWill17 分钟前
优化if else过多的方案(含设计模式处理方式)
java·设计模式
Shining059619 分钟前
Triton & 九齿系列《Triton 练气术》
开发语言·人工智能·python·学习·其他·infinitensor
东离与糖宝21 分钟前
GraalVM+Project Leyden实战:Spring Boot应用原生编译,Serverless冷启动自由
java·人工智能