HTTP conditional GET

I am trying to check if the file was modified after the specified date. I found this. I am trying to use HttpURLConnection Java to execute a "conditional get", but I never get the status code 304 . which seemed necessary to me. 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()); 

Output:

 {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 tried today, but still don't return 304.

Wednesday, 28 Nov 2012 12:59:56 GMT

It should return 304, but as you can see, no, any help is assigned.

0
source share
2 answers

The file has been modified. Change the If-Modified-Since header to something after the Last-Modified result.

I tried (with curl), and this CDN seems to have date problems. In order to get a 304 response to an If-Modified-Since request, you need to specify the exact Last-Modified date (see Sun, October 28, 2012 16:44:54 GMT). Needless to say, this is the evil behavior of this CDN.

+3
source

you should 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); } 
0
source

All Articles