Http Client Caching

In my Android application, I am trying to cache an Http Client response. I am testing this task using the facebook api graph and have the following url: https://graph.facebook.com/riz.ahmed.52

For the first time, I will get "first_name" and show it. Then I change the name of my facebook profile and call the same link again. I expect to get the old / cached "first_name", but I get updated. The console always shows “The response came from the upstream server” when I call the URL.

My code for Http Client is as follows:

CacheConfig cacheConfig = new CacheConfig(); cacheConfig.setMaxCacheEntries(1000); cacheConfig.setMaxObjectSizeBytes(8192); //HttpClient httpclient = new CachingHttpClient(new DefaultHttpClient(), cacheConfig); DefaultHttpClient httpclient = new DefaultHttpClient(); HttpContext localContext = new BasicHttpContext(); // Updated code [START] httpclient.addResponseInterceptor(new HttpResponseInterceptor() { public void process( final HttpResponse response, final HttpContext context) throws HttpException, IOException { response.removeHeader(response.getFirstHeader("Pragma")); response.removeHeader(response.getFirstHeader("Expires")); } }); // Updated code [END] HttpGet httpget = new HttpGet(url); // Execute HTTP Get Request HttpResponse response = httpclient.execute(httpget, localContext); HttpEntity entity = response.getEntity(); String res = EntityUtils.getContentCharSet(entity); CacheResponseStatus responseStatus = (CacheResponseStatus) localContext.getAttribute( CachingHttpClient.CACHE_RESPONSE_STATUS); switch (responseStatus) { case CACHE_HIT: System.out.println("A response was generated from the cache with no requests " + "sent upstream"); break; case CACHE_MODULE_RESPONSE: System.out.println("The response was generated directly by the caching module"); break; case CACHE_MISS: System.out.println("The response came from an upstream server"); break; case VALIDATED: System.out.println("The response was generated from the cache after validating " + "the entry with the origin server"); break; } 

I am using Android 2.3.3 . Please let me know what I am missing here.

+6
source share
1 answer

The loaded page defines the heading Expires:Sat, 01 Jan 2000 00:00:00 GMT , i.e. it is always considered obsolete and should always be re-selected.

Edit

Also returns Pragma: no-cache . Basically, this says that your HTTP client never caches this page. You can remove these headers with the HttpResponseInterceptor if you are configured to cache the response.

# 2 Edit :

Using http-clientcache-4.2.jar will be problematic, since it is not fully compatible with the version of the HTTP client packed with the Android SDK - you will get NoClassDefFoundError and similar nonsense when using it.

However, if you “create your own” by downloading the source for clientcache-4.2 and deleting any broken links (for example, refactoring the package name to maintain a public log) and destroying all annotations sprinkled throughout the code (etc.), you can get a working version. If you do this, it worked:

 class MakeCacheable implements HttpResponseInterceptor { public static MakeCacheable INSTANCE = new MakeCacheable(); public void process(HttpResponse resp, HttpContext ctx) throws HttpException, IOException { resp.removeHeaders("Expires"); resp.removeHeaders("Pragma"); resp.removeHeaders("Cache-Control"); } } 

It is DefaultHttpClient in the DefaultHttpClient used by CachingHttpClient as follows:

 DefaultHttpClient realClient = new DefaultHttpClient(); realClient.addResponseInterceptor(MakeCacheable.INSTANCE, 0); // This goes first CachingHttpClient httpClient = new CachingHttpClient(realClient, cacheConfig); 

If the record is cached or not, ResponseCachingPolicy is determined, unfortunately, it is final in the CachingHttpClient , but when viewed through it, all the headers that need to be made to record without caching will be cached.

+4
source

Source: https://habr.com/ru/post/916889/


All Articles