AppEngine gzip 压缩
我正在尝试对来自 GAE 服务器的响应进行 gzip 压缩,但在内容编码中收到 null。
我有以下代码:
connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type",
"application/json; charset=utf-8"); //"application/json; charset=utf-8"
connection.setRequestProperty("Accept-Encoding", "gzip");
connection.setRequestProperty("User-Agent", "gzip");
connection.setUseCaches (false);
connection.setDoInput(true);
connection.setDoOutput(true);
//write
//read
System.out.println("Content-Encoding " + connection.getContentEncoding());
我读到在 GAE 服务器上会自动压缩。那么问题出在哪里呢?
I'm trying to gzip responses from GAE server, but receive null in Content-Encoding.
I have the following code:
connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type",
"application/json; charset=utf-8"); //"application/json; charset=utf-8"
connection.setRequestProperty("Accept-Encoding", "gzip");
connection.setRequestProperty("User-Agent", "gzip");
connection.setUseCaches (false);
connection.setDoInput(true);
connection.setDoOutput(true);
//write
//read
System.out.println("Content-Encoding " + connection.getContentEncoding());
I've read that on GAE servers do compressing automatically. So what can be the problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
App Engine 前端服务器依赖于许多因素,包括
Accept-Encoding
和User-Agent
标头来确定是否应压缩响应。他们这样做是因为有许多用户代理声称接受压缩响应,但实际上无法理解它们。尝试将您的用户代理设置为合理的值(而不是“gzip”,它不是真正的用户代理),然后看看这是否有任何区别。
The App Engine frontend servers rely on a number of factors, including the
Accept-Encoding
andUser-Agent
headers to determine if they should compress responses. They do this because there are a number of user agents out there that claim to accept gzipped responses, but actually can't understand them.Try setting your user agent to something sensible (and not 'gzip', which isn't a real user agent), and see if that makes any difference.
就我而言,问题在于 servlet 没有为
Content-Type
标头指定值。我所需要做的就是明确指定它:In my case, the problem was that the servlet wasn't specifying a value for the
Content-Type
header. Specifying it explicitly was all I needed to do: