java 我可以使用 GZIP 压缩 HTTP 请求吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1450151/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 16:34:49  来源:igfitidea点击:

Can I compress HTTP Requests using GZIP?

javajava-mehttp-compression

提问by Kevin Boyd

I am communicating to a Tomcat Server using a Java ME application on my mobile device.
I was wondering if I could compress my requests/responses using Gzip to reduce the number of bytes sent over the network.

我正在使用移动设备上的 Java ME 应用程序与 Tomcat 服务器通信。
我想知道是否可以使用 Gzip 压缩我的请求/响应以减少通过网络发送的字节数。

回答by ZZ Coder

Modern phones have so much CPU power and the network is relatively slow so compression makes perfect sense. It's quite easy to do also.

现代手机拥有如此强大的 CPU 能力,而且网络速度相对较慢,因此压缩非常有意义。这也很容易做到。

On the J2ME side, you do something like this (assuming you use HttpConnection),

在 J2ME 方面,你做这样的事情(假设你使用 HttpConnection),

  hc.setRequestProperty("Accept-Encoding", "gzip, deflate");
  if (hc.getResponseCode() == HttpConnection.HTTP_OK) {
      InputStream in = hc.openInputStream();
      if ("gzip".equals(hc.getEncoding())) 
         in = new GZIPInputStream(in);
  ...

We use GZIPInputStream from tinyline but I am sure there are others,

我们使用来自 tinyline 的 GZIPInputStream 但我确定还有其他的,

http://www.tinyline.com/utils/index.html

http://www.tinyline.com/utils/index.html

On the server side, it's all built-in. Just add following attributes to the Connector in server.xml on Tomcat,

在服务器端,它都是内置的。只需在 Tomcat 上的 server.xml 中的连接器中添加以下属性,

<Connector 
compression="on"
compressionMinSize="2048"
compressableMimeType="text/html,application/json"
... />

回答by Stephen C

You can compress the content of an HTTP request or response, but not the headers. See section 3.6 of the HTTP 1.1 spec, and the later section that describes the Content-Encoding header.

您可以压缩 HTTP 请求或响应的内容,但不能压缩标头。请参阅 HTTP 1.1 规范的第 3.6 节,以及后面描述 Content-Encoding 标头的部分。

EDIT: The flip side of this is that there is no guarantee that an HTTP server side will accept any particular compression format. And depending on the quality of the server-side implementation of HTTP, it might not even recognize that the request content has been compressed. So you don't want to do this unless you know that the server-side supports compressed request content.

编辑:另一方面,不能保证 HTTP 服务器端会接受任何特定的压缩格式。并且根据 HTTP 的服务器端实现的质量,它甚至可能无法识别请求内容已被压缩。所以除非你知道服务器端支持压缩的请求内容,否则你不想这样做。

回答by Vineet Reynolds

Since you are using Tomcat, consider the possibility of putting an instance of Apache HTTP Server in front of the Tomcat server.

由于您使用的是 Tomcat,请考虑在 Tomcat 服务器前面放置一个 Apache HTTP Server 实例的可能性。

This can be done using the mod_jkmodule for Apache HTTP Server. Once you have done that, you can use mod_gzip/mod_deflatein Apache.

这可以使用Apache HTTP Server的mod_jk模块来完成。完成后,您可以在 Apache 中使用mod_gzip/ mod_deflate

Of course, your client should have the ability to handle the compressed responses, for this to work. If you force your client to work with compressed responses, the client will end up displaying gibberish, since it would have been (usually) expecting plain text responses. You will find the definite indicator of the client's capability to handle compressed responses, in the client's Accept-Encoding headers.

当然,您的客户端应该能够处理压缩的响应,以使其正常工作。如果您强制您的客户端使用压缩响应,则客户端最终将显示乱码,因为它(通常)会期望纯文本响应。您将在客户端的 Accept-Encoding 标头中找到客户端处理压缩响应能力的明确指示符。

This can be done programatically, using a servlet or a servlet filter that writes to a ZipOutputStream or GZipOutputStream, if you want to avoid the introduction of the Apache HTTP Server in the network. You will find some pointers on how to do this at the OReilly OnJava.com site.

如果您想避免在网络中引入 Apache HTTP 服务器,可以使用写入 ZipOutputStream 或 GZipOutputStream 的 servlet 或 servlet 过滤器以编程方式完成此操作。您可以在 OReilly OnJava.com 站点上找到有关如何执行此操作的一些指示。

回答by Mark K

On the server side, you can enable it as described here, but the mobile application will need a library that can decompress gzip, such as this one. Might be a bit of work to get it working though...

在服务器端,您可以按照此处所述启用它,但移动应用程序将需要一个可以解压缩 gzip 的库,例如这个。虽然可能需要一些工作才能让它工作......