jum*_*noz 8 java browser cookies gzip
我试图通过Gzipping来自后端的所有响应来减少网络流量,但我遇到了一个问题,如果登录/注销调用以相同的方式完成并且我不确定原因,那么浏览器中没有设置cookie.
这是Java中的后端代码.
Cookie cookie = new Cookie("cookie_name", cookieToken);
cookie.setPath("/");
cookie.setMaxAge(Integer.MAX_VALUE);
response.addCookie(cookie);
response.setContentType("application/json; charset=utf-8");
response.setStatus(HttpServletResponse.SC_OK);
response.setHeader("Content-Encoding", "gzip");
String json=serviceOutput.toString();
byte[] gzip = Utils.gzip(json.getBytes("UTF-8"));
response.setContentLength(gzip.length);
response.getOutputStream().write(gzip);
Run Code Online (Sandbox Code Playgroud)
任何指针为什么这不起作用?我很确定我之前做过同样的事情没有任何问题.
-
public static byte[] gzip(byte[] content) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
GZIPOutputStream g = new GZIPOutputStream(baos);
if (content != null && content.length > 0) {
g.write(content, 0, content.length);
g.close();
}
return baos.toByteArray();
}
Run Code Online (Sandbox Code Playgroud)
我已经修改了你的代码来模拟数据
package com.mytests.pack;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.zip.GZIPOutputStream;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HomeServlet extends HttpServlet{
private static final long serialVersionUID = -2638972063792556071L;
private Cookie[] cookies;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
cookies = request.getCookies();
System.out.println("----------------------======================-------------------");
if(null!=cookies){
for(Cookie c : cookies){
System.out.println(c.getValue());
}
}else {
System.out.println("First request");
}
System.out.println("----------------------======================-------------------");
Cookie cookie = new Cookie("cookie_name", "cookiecontentishereofname"+LocalDateTime.now().toString());
cookie.setPath("/");
cookie.setMaxAge(Integer.MAX_VALUE);
response.setContentType("application/json; charset=utf-8");
response.setStatus(HttpServletResponse.SC_OK);
response.addCookie(cookie);
response.setHeader("Content-Encoding", "gzip");
String json="{\"keyone\":\"Somestring is here in the point\"}";
ByteArrayOutputStream obj=new ByteArrayOutputStream();
GZIPOutputStream gzipstream = new GZIPOutputStream(obj);
byte[] gzip = json.getBytes("UTF-8");
gzipstream.write(gzip);
gzipstream.close();
response.setContentLength(obj.toByteArray().length);
response.getOutputStream().write(obj.toByteArray());
}
}
Run Code Online (Sandbox Code Playgroud)
与上面的代码和码头-分布- 9.4.0.v20161208
经过测试的浏览器
- 谷歌浏览器 -Version 58.0.3029.110 (64-bit)
RESULT :cookie was set
- 歌剧 -45.0.2552.812 (PGO)
RESULT :cookie was set
- Mozilla Firefox -53.0.3 (32-bit)
关于FIREFOX的重要注意事项
当您打开开发人员工具时,它不会显示任何cookie集,但实际上可以正常查看截图
在哪里
Jetty控制台输出显示cookie已成功设置!
配置中的其他更改我想告诉你
添加POM dependency snippet
为
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>compile</scope>
</dependency>
</dependencies>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
714 次 |
最近记录: |