headers java_Java Response.headers方法代码示例

这段代码展示了如何根据响应头信息(如Date, Expires, Last-Modified, ETag等)来创建OkHttp的缓存策略。它解析HTTP日期,提取缓存响应的相关字段,并用于确定请求是否可以被缓存及其有效期。
摘要由CSDN通过智能技术生成

import com.squareup.okhttp.Response; //导入方法依赖的package包/类

public CacheStrategy$Factory(long nowMillis, Request request, Response cacheResponse) {

this.nowMillis = nowMillis;

this.request = request;

this.cacheResponse = cacheResponse;

if (cacheResponse != null) {

Headers headers = cacheResponse.headers();

int size = headers.size();

for (int i = 0; i < size; i++) {

String fieldName = headers.name(i);

String value = headers.value(i);

if ("Date".equalsIgnoreCase(fieldName)) {

this.servedDate = HttpDate.parse(value);

this.servedDateString = value;

} else if ("Expires".equalsIgnoreCase(fieldName)) {

this.expires = HttpDate.parse(value);

} else if ("Last-Modified".equalsIgnoreCase(fieldName)) {

this.lastModified = HttpDate.parse(value);

this.lastModifiedString = value;

} else if ("ETag".equalsIgnoreCase(fieldName)) {

this.etag = value;

} else if ("Age".equalsIgnoreCase(fieldName)) {

this.ageSeconds = HeaderParser.parseSeconds(value, -1);

} else if (OkHeaders.SENT_MILLIS.equalsIgnoreCase(fieldName)) {

this.sentRequestMillis = Long.parseLong(value);

} else if (OkHeaders.RECEIVED_MILLIS.equalsIgnoreCase(fieldName)) {

this.receivedResponseMillis = Long.parseLong(value);

}

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值