如何使用Expires避免浏览器请求

时间:2013-11-21 09:09:16

标签: http-headers cache-control

我正在编写一个Java Web服务,用于Web优化的培训目的。我的Web服务发送的响应将在365天后到期

@Path("cache2")
public class WsCache {
    @GET
        @Produces("application/json")
    public Response expires(){
        System.out.println("Expires in 365 days");
        String later = getDaysAfterHttpDate(365);
        return Response.ok("{Hello:hello}").header("Expires",later).build();
    }
}

问题是Chrome或Firefox使用缓存控制提出请求:

GET /fora-comments-ejb/api/cache2 HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Cache-Control: max-age=0
Accept: text/html...
User-Agent: Mozilla/5.0 ...
Accept-Encoding: gzip,deflate,sdch
Accept-Language: fr-FR,fr;...

因为max-age的任何值都会覆盖Expires,有没有办法让firefox / Chrome不对第二个请求使用缓存控制?

1 个答案:

答案 0 :(得分:0)

通常,我稍后发现answer on Stack.你需要一个真正的链接来缓存,或者浏览器发送一个max-cache = 0,在我看来 - 一个好主意。

我只需要使用以下链接创建一个html文件:

<a href="fora-comments-ejb/api/cache">Fetch some JSON<a/>

并且有效

enter image description here