Reputation: 470
I'm trying to check if a file has been modified after a given date. I found this I'm trying to use Java's HttpURLConnection to do a "conditional get", but I never get a 304 status code. which seemed like what I needed. But if I try:
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());
The output is:
{null=[HTTP/1.1 200 OK], ETag=["087588e2bb5cd1:0"],
Date=[Wed, 28 Nov 2012 12:39:31 GMT], Content-Length=[1150],
Last-Modified=[Sun, 28 Oct 2012 16:44:54 GMT], Accept-Ranges=[bytes],
Connection=[keep-alive], Content-Type=[image/x-icon], X-Cache=[HIT],
Server=[NetDNA-cache/2.2], Cache-Control=[max-age=604800]}
Edit
I've tried today's date but still doesn't return 304.
Wed, 28 Nov 2012 12:59:56 GMT
It should return 304 but as you can see it doesn't, any help is appropriated.
Upvotes: 0
Views: 2762
Reputation: 151
you are supposed to use something like this...
connection.addRequestProperty("If-Modified-Since", lastModified);
Here is the Code:
try {
HttpResponseCache cache = responseCache.getInstalled();
cacheResponse = cache.get(uri, "GET",
new HashMap<String, List<String>>());
if (cacheResponse != null) {
Map<String, List<String>> headers = cacheResponse
.getHeaders();
List<String> eTagHeader = headers.get("ETag");
eTag = eTagHeader.get(0);
if (eTag != null) {
connection.addRequestProperty("If-None-Match", eTag);
}
if(headers.containsKey("Last-Modified")){
List<String> lastModifiedList = headers.get("Last-Modified");
if(null != lastModifiedList && !lastModifiedList.isEmpty()){
String lastModified = lastModifiedList.get(0);
if(null != lastModified){ connection.addRequestProperty("If-Modified-Since", lastModified);
}
}
}
}
} catch (IOException e1) {
Log.e(TAG, String.format("Cannot read from cache.", url),
e1);
}
Upvotes: 0
Reputation: 18960
The file has been modified. Change your If-Modified-Since
header to something after the Last-Modified
result.
I tried it (with curl) and this CDN seems to have trouble with dates. To get a 304
response to an If-Modified-Since
request, you need to provide the exact Last-Modified
date (Sun, 28 Oct 2012 16:44:54 GMT here). Needless to say this is evil behaviour from this CDN.
Upvotes: 3