我可以使用GZIP压缩HTTP请求吗? [英] Can I compress HTTP Requests using GZIP?

查看:392
本文介绍了我可以使用GZIP压缩HTTP请求吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用移动设备上的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.

推荐答案

现代手机拥有如此多的CPU功率,网络相对较慢,因此压缩非常有意义。这也很容易。

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.

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

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

我们使用来自tinyline的GZIPInputStream,但我相信还有其他人,

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

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

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

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

这篇关于我可以使用GZIP压缩HTTP请求吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆