5

I have an api request which returns the response and also the response headers Last-Modified and Date. I am using HttpUrlConnection for making the HTTP.GET requests. I am also using HttpResponseCache to cache the responses.

When the server returns a response code 200 ,the response is cached. I'm facing two issues now.

First : When the second time the api is requested, HttpUrlConnection sets the 'Date' header's value as the 'If-Modified-Since' header instead of using the 'Last-Modified' header's value.

I solved this issue by manually setting the If-Modified-Since header from the cached response. So now the server returns 304 the second time api is requested.

Here comes my second issue.

Second : Normally if the server returns 304 and the response is cached ,then HttpUrlConnection returns the cached response and the response code will be 200. This works as desired in the case of the api response which has ETag header in the response. But for the responses with only Last-Modified header , the HttpUrlConnection returns the response code as 304 itself and do not return the cached response.

Has any one encountered a similar issue ?

Please find below the java implementation for the api request.

URL url = new URL(this.url);
HttpURLConnection conn  = getProtocolType(url);
conn.setRequestMethod("GET");
conn.setReadTimeout(timeOut);
conn.setConnectTimeout(timeOut);
conn.setInstanceFollowRedirects(true);
conn.setRequestProperty("User-Agent", this.userAgent);
conn.setRequestProperty("Cache-Control", "max-age=0");
conn.setUseCaches(true);
conn.setDefaultUseCaches(true);
conn.connect();
this.responseCode = conn.getResponseCode();

0 Answers0