gpt4 book ai didi

Android HttpResponseCache 不工作

转载 作者:行者123 更新时间:2023-11-29 01:44:12 28 4
gpt4 key购买 nike

我正在尝试使用 HttpResponseCache,但没能成功。

我是这样安装的:

        SdCardManager manager = SdCardManager.getSdCardManager(context);
File dir = new File(manager.getAbsolutePathForSdCardFile(""), "http");
Log.e("Test", "Installing cache to " + dir.getAbsolutePath());
HttpResponseCache.install(dir, 10 * 1024 * 1024);

我的连接代码是:

    try{
URL url = new URL(mUrl);

HttpURLConnection connection = (HttpURLConnection) ((mProxy == null) ? url
.openConnection() : url.openConnection(mProxy.getProxy()));

HttpResponseCache cache = HttpResponseCache.getInstalled();
Log.e("Test", "Req count: " + cache.getRequestCount() + " hit count " + cache.getHitCount() + " size = " + cache.size());

connection.setUseCaches(true);
connection.setDefaultUseCaches(true);
connection.addRequestProperty("Cache-Control", "max-stale=" + (60 * 60 * 24 * 28));

applyHeaders(connection);
connection.setRequestMethod("GET");
connection.setConnectTimeout(mTimeout);
connection.setReadTimeout(mTimeout);

connection.connect();

mStatusCode = connection.getResponseCode();
mStatusMessage = connection.getResponseMessage();

connection.disconnect();
}
catch (IOException ex) {
ex.printStackTrace();
return false;
}

在 adb shell 中查看,我看到正在创建缓存目录并且它有一个日志文件。但是,不再有数据发送到目录:

shell@maguro:/storage/emulated/legacy/Android/data/com.snip.pre/files/http $ ls
journal

我在每次请求后刷新缓存:

HttpResponseCache cache = HttpResponseCache.getInstalled();
cache.flush();

知道我做错了什么吗?服务器响应 header 在这里:

GET /imgv.oms/0.0.1fd02.a7003c2a HTTP/1.1
Cache-Control: max-stale=2419200
Authorization: Basic bWFydGlubDppc2lz
X-Loginpageredirect: none
Accept: image/*
Accept-Encoding: gzip,deflate
Accept-Language: en-GB
User-Agent: Dalvik/1.6.0 (Linux; U; Android 4.3; Galaxy Nexus Build/JWR66Y)
Host: appsdev2.server.com:81
Connection: Keep-Alive
Cookie: POSESSIONID=F8654323-f7frSyNmAGKsvE4nFXZJQIIy3Io

有什么东西阻止它被缓存吗?

最佳答案

问题是我发送了 Authorization header ,而标准对此类请求是否可缓存有特定规则。见

http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html 14.8节

服务器不允许我缓存结果,正如 njzk2 所暗示的那样。

关于Android HttpResponseCache 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22486665/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com