I'm writing a client that is making repeated http requests for xml data that is changing over time. It looks like the Android stack is caching my page requests and returning the same page repeatedly. How do I make sure it gets a fresh page each time?
-- code ---
HttpClient client = new DefaultHttpClient(); HttpGet request = new HttpGet(url); HttpResponse response; response = client.execute(request); InputStream in; in = response.getEntity().getContent(); BufferedReader reader = new BufferedReader(new InputStreamReader(in));
Thanks, Gerry
A proxy HTTP server that forwards your message to the server would never cache anything but a GET or a HEAD request.
To measure cache effectiveness, this class tracks three statistics: Request Count: the number of HTTP requests issued since this cache was created. Network Count: the number of those requests that required network use. Hit Count: the number of those requests whose responses were served by the cache.
The Cache-Control HTTP header field holds directives (instructions) — in both requests and responses — that control caching in browsers and shared caches (e.g. Proxies, CDNs).
android.net.http.HttpResponseCache. Caches HTTP and HTTPS responses to the filesystem so they may be reused, saving time and bandwidth. This class supports HttpURLConnection and HttpsURLConnection ; there is no platform-provided cache for DefaultHttpClient or AndroidHttpClient .
Append an unused parameter on the end of the URL:
HttpGet request = new HttpGet(url + "?unused=" + someRandomString());
where someRandomString()
probably involves the current time.
It's crude, but it's pretty much guaranteed to work regardless of all the outside factors that can make a "proper" solution fail, like misconfigured or buggy proxies.
add a HTTP header:
Cache-Control: no-cache
and see if that works.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With