use of com.lzy.okgo.model.HttpHeaders in project okhttp-OkGo by jeasonlzy.
the class HeaderParser method addCacheHeaders.
/**
* 对每个请求添加默认的请求头,如果有缓存,并返回缓存实体对象
* Cache-Control: max-age=0 以秒为单位
* If-Modified-Since: Mon, 19 Nov 2012 08:38:01 GMT 缓存文件的最后修改时间。
* If-None-Match: "0693f67a67cc1:0" 缓存文件的ETag值
* Cache-Control: no-cache 不使用缓存
* Pragma: no-cache 不使用缓存
* Accept-Language: zh-CN,zh;q=0.8 支持的语言
* User-Agent: 用户代理,它的信息包括硬件平台、系统软件、应用软件和用户个人偏好
* Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36
*
* @param request 请求类
* @param cacheEntity 缓存实体类
* @param cacheMode 缓存模式
*/
public static <T> void addCacheHeaders(BaseRequest request, CacheEntity<T> cacheEntity, CacheMode cacheMode) {
//1. 按照标准的 http 协议,添加304相关请求头
if (cacheEntity != null && cacheMode == CacheMode.DEFAULT) {
HttpHeaders responseHeaders = cacheEntity.getResponseHeaders();
if (responseHeaders != null) {
String eTag = responseHeaders.get(HttpHeaders.HEAD_KEY_E_TAG);
if (eTag != null)
request.headers(HttpHeaders.HEAD_KEY_IF_NONE_MATCH, eTag);
long lastModified = HttpHeaders.getLastModified(responseHeaders.get(HttpHeaders.HEAD_KEY_LAST_MODIFIED));
if (lastModified > 0)
request.headers(HttpHeaders.HEAD_KEY_IF_MODIFIED_SINCE, HttpHeaders.formatMillisToGMT(lastModified));
}
}
}
Aggregations