Cookie压缩

2018-01-02  本文已影响0人  何甜甜在吗

在介绍cookie压缩之前先来介绍一下cookie保存中文,因为在压缩cookie时要将结果进行转码好像就是这个原因
原本以为cookie保存中文,顶多在取值时会出现乱码,然而并不是这样的,写了个测试demo
TestEncode.java

public class TestEncode extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Cookie[] cookies = req.getCookies();

        for (Cookie c : cookies) {
            System.out.println(c.getName());
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Cookie c = new Cookie("姓名", "皮皮甜");
        resp.addCookie(c);
        doPost(req, resp);
    }
}

在web.xml进行了url映射配置,然后,做测试就报错了

HTTP Status 500 - Cookie name "姓名" is a reserved token
type Exception report

message Cookie name "姓名" is a reserved token

description The server encountered an internal error that prevented it from fulfilling this request.

exception

java.lang.IllegalArgumentException: Cookie name "姓名" is a reserved token
    javax.servlet.http.CookieNameValidator.validate(Cookie.java:406)
    javax.servlet.http.Cookie.<init>(Cookie.java:120)
    com.cqupt.javaweb.TestEncode.doGet(TestEncode.java:26)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    com.cqupt.javaweb.EncodingFilter.doFilter(EncodingFilter.java:29)
note The full stack trace of the root cause is available in the Apache Tomcat/8.0.30 logs.

中文果然不可以,因为中文的编码方式是Unicode。在servlet中创建Cookie必须遵守RFC 2109规范。RFC 2019规范里面就是key,value必须是ascii字符,或base64。将key和value换成英文以后就成功了,但是如果非要使用中文,也是有方法可以做到的。
Cookie中使用Unicode字符时需要对Unicode字符进行编码,编码可以使用URLEncoder类的encode(String str, String encoding)方法,解码使用URLDecoder类的decode(String str, String encoding)方法。
哪里失败就从哪里爬起,继续改代码

public class TestEncode extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Cookie[] cookies = req.getCookies();

        for (Cookie c : cookies) {
            String cookieName = URLDecoder.decode(c.getName(), "gbk");
            String cookieValue = URLDecoder.decode(c.getValue(), "gbk");
            System.out.println(cookieName + " : " + cookieValue);
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Cookie c = new Cookie(URLEncoder.encode("姓名"), URLEncoder.encode("皮皮甜"));
        resp.addCookie(c);
//        resp.setCharacterEncoding("UTF-8");
        doPost(req, resp);
    }
}

程序运行结果:

姓名 : 皮皮甜

现在进入cookie压缩压缩介绍
为什么需要进行cookie压缩,因为cookie是保存在http的头部的,服务器和客户端进行交互时,需要进行cookie传输,如果cookie数量非常多、并且内容特别多的时候,无形增大了数据传输量,会占用很大的网络带宽。进行压缩,可以减少数据量。
cookie压缩:将cookie的多个k/v对看成普通的文本,做文本压缩。压缩算法可以使用gzip和deflate算法,因为cookie规范中,cookie中不能包含控制字符(压缩以后会出现==这些控制字符串),所以需要对压缩后的结果进行转码,可以使用Base32或者Base64。
因为使用Base64进行编码的时候会出现一些问题,所以选择了URLEncode进行了编码

demo:在向客户端发送cookie时就进行压缩
 /**
     * 服务器向客户端颁发cookie,在颁发给客户端时先对cookie进行压缩
     * @param response
     * @param name cookie名字
     * @param value cookie值
     * @throws IOException
     * */
    public static void addCookie(HttpServletResponse response, String name, String value) throws IOException {
        System.out.println("进入了方法体...");


        //进行cookie进行压缩
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        DeflaterOutputStream dos = new DeflaterOutputStream(bos);

        dos.write(name.getBytes());
        dos.close();
        System.out.println("name: " + name);
        System.out.println("name before compress length: " + name.length());
        String compressName = URLEncoder.encode(new String(bos.toByteArray()), "UTF-8");
//        String compressName = new BASE64Encoder().encode(bos.toByteArray());
        System.out.println("name after compress length: " + compressName.length());
        System.out.println("compressName: " + compressName);

        ByteArrayOutputStream bos1 = new ByteArrayOutputStream();
        DeflaterOutputStream dos1 = new DeflaterOutputStream(bos1);

        dos1.write(value.getBytes());
        dos1.close();
        System.out.println("value: " + value);
        System.out.println("value before compress length: " + value.length());
        //压缩以后进行编码
        String compressValue = URLEncoder.encode(new String(bos.toByteArray()), "UTF-8");
        System.out.println("value after compress length: " + compressValue.length());
        System.out.println("compressValue: " + compressValue);

        //压缩以后需要使用使用base64进行编码,因为压缩以后存在控制字符,不符合cookie标准
    //        String lastName = URLEncoder.encode(compressName, "UTF-8");
    //        String lastValue = URLEncoder.encode(compressValue, "UTF-8");
    //        System.out.println("lastName: " + lastName);
    //        System.out.println("lastValue: " + lastValue);

        Cookie cookie = new Cookie(compressName, compressValue);
        cookie.setPath("/");  //本域名下contextPath都可以访问该Cookie

        cookie.setMaxAge(Integer.MAX_VALUE);  //失永远保存在浏览器,除非人为进行清理
        System.out.println("发送结束...");
        unCompressCookie(cookie);
        response.addCookie(cookie);  //向客户端发送cookie
    }
上一篇下一篇

猜你喜欢

热点阅读