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

发布于 2024-08-05 16:52:36 字数 89 浏览 6 评论 0原文

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

陌路终见情 2024-08-12 16:52:36

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

在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);
  ...

我们使用tinyline的GZIPInputStream,但我确信还有其他的,

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

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

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

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.

On the J2ME side, you do something like this (assuming you use 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,

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,

<Connector 
compression="on"
compressionMinSize="2048"
compressableMimeType="text/html,application/json"
... />
浮世清欢 2024-08-12 16:52:36

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

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

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.

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.

软的没边 2024-08-12 16:52:36

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

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

当然,您的客户端应该有能力处理压缩响应,这样才能发挥作用。如果您强迫客户端使用压缩响应,客户端最终将显示乱码,因为它(通常)期望纯文本响应。您将在客户端的 Accept-Encoding 标头中找到客户端处理压缩响应的能力的明确指示符。

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

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

This can be done using the mod_jk module for Apache HTTP Server. Once you have done that, you can use mod_gzip/mod_deflate in Apache.

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.

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.

流绪微梦 2024-08-12 16:52:36

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

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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文