Search in sources :

Example 6 with HttpHeaders

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));
        }
    }
}
Also used : HttpHeaders(com.lzy.okgo.model.HttpHeaders)

Aggregations

HttpHeaders (com.lzy.okgo.model.HttpHeaders)6 CacheMode (com.lzy.okgo.cache.CacheMode)2 IOException (java.io.IOException)2 ContentValues (android.content.ContentValues)1 CacheEntity (com.lzy.okgo.cache.CacheEntity)1 AbsCallbackWrapper (com.lzy.okgo.callback.AbsCallbackWrapper)1 PersistentCookieStore (com.lzy.okgo.cookie.store.PersistentCookieStore)1 OkGoException (com.lzy.okgo.exception.OkGoException)1 HttpParams (com.lzy.okgo.model.HttpParams)1 Response (com.lzy.okgo.model.Response)1 BaseRequest (com.lzy.okgo.request.BaseRequest)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1 SocketTimeoutException (java.net.SocketTimeoutException)1 CertificateException (java.security.cert.CertificateException)1 StringTokenizer (java.util.StringTokenizer)1 Request (okhttp3.Request)1 RequestBody (okhttp3.RequestBody)1