2016-02-05 54 views
3

我用OkHttpClient(V3.0.1),這是在我的應用程序,但ETag的和如果無 - 匹配不工作如何使用OkHttp的ETag的和如果 - 無 - 匹配在Android

,例如:

首先 GET 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 

得到同樣的網址: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(); 

預先感謝任何幫助。

+0

你的代碼似乎很好。嘗試使用代理設置通過使用mitmproxy或charlesproxy等軟件來嗅探請求,因爲改造logcat不會打印if-none-match頭,儘管okhttp已經添加了頭。另一件事是翻新打印304狀態碼爲200。 – baskara

回答

0

您是否正在閱讀第一個請求的完整響應正文? OkHttp不會將響應寫入緩存,直到完成讀取正文爲止。