【发布时间】:2014-01-04 22:04:00
【问题描述】:
我正在下载一个 JAR 文件,并且想使用 If-Modified-Since,所以如果我不需要它,我不会得到整个文件,但由于某种原因,我的香草 Apache (afaik) 不是t 正确返回 304。
这是来自wireshark:
GET /whatever.jar HTTP/1.1
If-Modified-Since: Sat, 04 Jan 2014 21:46:26 GMT
User-Agent: Jakarta Commons-HttpClient/3.1
Host: example.com
HTTP/1.1 200 OK
Date: Sat, 04 Jan 2014 20:32:31 GMT
Server: Apache/2.2.4 (Unix) mod_ssl/2.2.4 OpenSSL/0.9.8e DAV/2 mod_jk/1.2.26 PHP/5.3.6 SVN/1.4.4
Last-Modified: Sat, 04 Jan 2014 19:13:14 GMT
ETag: "b6c037-1ddad9f-d17a6680"
Accept-Ranges: bytes
Content-Length: 31305119
Vary: User-Agent
Content-Type: text/plain
... [bunch of bytes] ...
我不需要指定其他标题,是吗?我是否缺少 Apache 正确读取此标头所需的模块?
还有其他想法或建议吗?
这是我的Java代码,供参考:
File jarFile = new File(filePath);
GetMethod get = new GetMethod(downloadUrl);
Date lastModified = new Date(jarFile.lastModified());
get.setRequestHeader("If-Modified-Since", DateUtil.formatDate(lastModified));
HttpClient client = new HttpClient();
int code = client.executeMethod(get);
更新:解决方案
If-Modified-Date 需要与服务器完全匹配,我通过在下载的文件上显式设置 lastModifiedDate 来实现这一点:
String serverModified = get.getResponseHeader("Last-Modified").getValue();
jarFile.setLastModified(DateUtil.parseDate(serverModified).getTime());
执行此操作后,后续调用将不会下载该文件。
【问题讨论】:
标签: java apache http-headers cache-control apache-httpcomponents