我使用应用程序中的okhttpclient(v3.0.1),但是etag和if none match不起作用。
例如:
首先获得http://112.4.19.67/task/imags_avatar/20130607165126605.png
作为回应,我得到了ETag和最后一次修改。
请求:
GET /task/imags_avatar/20130607165126605.png HTTP/1.1
Host: 112.4.19.67
Connection: Keep-Alive
Accept-Encoding: gzip
User-Agent: okhttp/3.0.1
回应:
HTTP/1.1 200 OK
Server: Apache
Last-Modified: Mon, 30 Jun 2014 04:44:43 GMT
ETag: "1205-4fd06515d9572"
Content-Type: image/png
Content-Length: 4613
Accept-Ranges: bytes
Date: Thu, 04 Feb 2016 13:30:05 GMT
X-Varnish: 3684240013 3683622711
Age: 1313
Via: 1.1 varnish
Connection: keep-alive
第二个获得相同的url:http://112.4.19.67/task/imags_avatar/20130607165126605.png
我想我可以看看是否没有符合要求的。但它并不存在。
请求:
GET /task/imags_avatar/20130607165126605.png HTTP/1.1
Host: 112.4.19.67
Connection: Keep-Alive
Accept-Encoding: gzip
User-Agent: okhttp/3.0.1
回应:
HTTP/1.1 200 OK
Server: Apache
Last-Modified: Mon, 30 Jun 2014 04:44:43 GMT
ETag: "1205-4fd06515504c0"
Content-Type: image/png
Content-Length: 4613
Accept-Ranges: bytes
Date: Thu, 04 Feb 2016 13:30:26 GMT
X-Varnish: 3389168698 3388862573
Age: 642
Via: 1.1 varnish
Connection: keep-alive
我的密码
File file = getCacheDir();
Cache cache = new Cache(file, CACHE_SIZE);
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.cache(cache);
OkHttpClient client = builder.build();
Request request = new Request.Builder()
.url(url)
.build();
提前谢谢你的帮助。
最佳答案
您是否阅读了第一个请求的完整响应正文?在完成对主体的读取之前,okhttp不会将响应写入缓存。