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

相关推荐
2401_8574396918 分钟前
SSM 架构下 Vue 电脑测评系统:为电脑性能评估赋能
开发语言·php
SoraLuna1 小时前
「Mac畅玩鸿蒙与硬件47」UI互动应用篇24 - 虚拟音乐控制台
开发语言·macos·ui·华为·harmonyos
xlsw_1 小时前
java全栈day20--Web后端实战(Mybatis基础2)
java·开发语言·mybatis
神仙别闹2 小时前
基于java的改良版超级玛丽小游戏
java
Dream_Snowar2 小时前
速通Python 第三节
开发语言·python
黄油饼卷咖喱鸡就味增汤拌孜然羊肉炒饭2 小时前
SpringBoot如何实现缓存预热?
java·spring boot·spring·缓存·程序员
暮湫2 小时前
泛型(2)
java
超爱吃士力架2 小时前
邀请逻辑
java·linux·后端
南宫生3 小时前
力扣-图论-17【算法学习day.67】
java·学习·算法·leetcode·图论
转码的小石3 小时前
12/21java基础
java