AppEngine gzip 压缩

发布于 2024-12-21 02:35:15 字数 647 浏览 0 评论 0原文

我正在尝试对来自 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 技术交流群。

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

发布评论

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

评论(2

尬尬 2024-12-28 02:35:15

App Engine 前端服务器依赖于许多因素,包括 Accept-EncodingUser-Agent 标头来确定是否应压缩响应。他们这样做是因为有许多用户代理声称接受压缩响应,但实际上无法理解它们。

尝试将您的用户代理设置为合理的值(而不是“gzip”,它不是真正的用户代理),然后看看这是否有任何区别。

The App Engine frontend servers rely on a number of factors, including the Accept-Encoding and User-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.

山色无中 2024-12-28 02:35:15

就我而言,问题在于 servlet 没有为 Content-Type 标头指定值。我所需要做的就是明确指定它:

public void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws IOException, ServletException {
    ...
    resp.setHeader("Content-Type", "application/json");
    ...

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:

public void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws IOException, ServletException {
    ...
    resp.setHeader("Content-Type", "application/json");
    ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文