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

相关推荐
铸人5 分钟前
大数分解的Shor算法-C#
开发语言·算法·c#
yyjtx8 分钟前
DHU上机打卡D31
开发语言·c++·算法
莫寒清24 分钟前
Spring MVC:@PathVariable 注解详解
java·spring·mvc
rit843249924 分钟前
全变分正则化图像去噪的MATLAB实现
开发语言·matlab
勇往直前plus24 分钟前
python格式化字符串
开发语言·前端·python
AKA__Zas26 分钟前
初识基本排序
java·数据结构·学习方法·排序
未来之窗软件服务34 分钟前
AI人工智能(二十四)错误示范ASR张量错误C#—东方仙盟练气期
开发语言·人工智能·c#·仙盟创梦ide·东方仙盟
大黄说说35 分钟前
不是进阶阶梯,而是协作维度:重新理解 Claude Code 中的 Commands、Skills 与 Agents
开发语言
yong99901 小时前
基于C#实现的UPnP端口映射程序
开发语言·c#
Gogo11211 小时前
架构的宿命:深入对比 NestJS (Node.js) 与 Java 的垃圾回收机制
java·node.js