san*_*ity 5 java caching http http-caching
这是我的代码:
final HttpURLConnection conn = (HttpURLConnection) sourceURL.openConnection();
if (cachedPage != null) {
if (cachedPage.eTag != null) {
conn.setRequestProperty("If-None-Match", cachedPage.eTag);
}
conn.setIfModifiedSince(cachedPage.pageLastModified);
}
conn.connect();
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
newCachedPage.eTag = conn.getHeaderField("ETag");
newCachedPage.pageLastModified = conn.getHeaderFieldDate("Last-Modified", 0);
} else if (conn.getResponseCode() == HttpURLConnection.HTTP_NOT_MODIFIED) {
// Never reaches here
}
Run Code Online (Sandbox Code Playgroud)
我似乎永远不会得到HTTP_NOT_MODIFIED响应代码,甚至连续多次点击同一台服务器 - 页面肯定没有变化.此外,conn.getHeaderField("ETag")似乎总是响应null,有时conn.getHeaderFieldDate("Last-Modified",0)返回0.我已经尝试过针对各种Web服务器.
谁能告诉我我做错了什么?
Bal*_*usC 17
你们都依赖于服务器配置.
如果你得到一个Expires响应标题,那么它只是意味着你不需要在指定的过期时间之前请求任何东西.如果你得到一个Last-Modified响应标题,那么这意味着你应该能够If-Modified-Since用来测试它.如果你得到一个ETag响应标题,那么这意味着你应该能够If-None-Match用来测试它.
让我们以http://cdn3.sstatic.net/stackoverflow/img/favicon.ico为例(Stackoverflow的favicon图像):
URLConnection connection = new URL("http://cdn3.sstatic.net/stackoverflow/img/favicon.ico").openConnection();
System.out.println(connection.getHeaderFields());
Run Code Online (Sandbox Code Playgroud)
这给出了:
{null = [HTTP/1.1 200 OK],ETag = ["9d9bd8b1165cb1:0"],日期= [星期三,2011年8月17日17:57:07 GMT],Content-Length = [1150],Last-Modified = [ 2010年10月6日星期三02:53:46 GMT],Content-Type = [image/x-icon],Connection = [keep-alive],Accept-Ranges = [bytes],Server = [nginx/0.8.36] ,X-Cache = [HIT],Cache-Control = [max-age = 604800]}
现在,If-Modified-Since使用相同的值执行以下操作Last-Modified:
URLConnection connection = new URL("http://cdn3.sstatic.net/stackoverflow/img/favicon.ico").openConnection();
connection.setRequestProperty("If-Modified-Since", "Wed, 06 Oct 2010 02:53:46 GMT");
System.out.println(connection.getHeaderFields());
Run Code Online (Sandbox Code Playgroud)
这给出了预期的304:
{null = [HTTP/1.1 304未修改],ETag = ["9d9bd8b1165cb1:0"],日期= [Wed,2011年8月17日17:57:42 GMT],Last-Modified = [Wed,2010年10月6日02:格林威治标准时间53:46],Connection = [keep-alive],Server = [nginx/0.8.36],X-Cache = [HIT],Cache-Control = [max-age = 604800]}
现在,If-None-Match使用相同的值执行以下操作ETag:
URLConnection connection = new URL("http://cdn3.sstatic.net/stackoverflow/img/favicon.ico").openConnection();
connection.setRequestProperty("If-None-Match", "9d9bd8b1165cb1:0");
System.out.println(connection.getHeaderFields());
Run Code Online (Sandbox Code Playgroud)
这意外地给出了200:
{null = [HTTP/1.1 200 OK],ETag = ["9d9bd8b1165cb1:0"],日期= [星期三,2011年8月17日18:01:42 GMT],Content-Length = [1150],Last-Modified = [ 2010年10月6日星期三02:53:46 GMT],Content-Type = [image/x-icon],Connection = [keep-alive],Accept-Ranges = [bytes],Server = [nginx/0.8.36] ,X-Cache = [HIT],Cache-Control = [max-age = 604800]}
更令人惊讶的是,当两个标头都设置为随机垃圾值时ETag,服务器仍然提供304.这表明http://cdn3.sstatic.netIf-None-Match后面的服务器完全忽略了它.这可能是(代理)配置问题或完全清楚地完成(不明显的原因imho).
| 归档时间: |
|
| 查看次数: |
10481 次 |
| 最近记录: |