gpt4 book ai didi

http-headers - 当服务器使用Retrofit返回相同的响应时,如何避免解析?

转载 作者:行者123 更新时间:2023-12-04 04:30:01 25 4
gpt4 key购买 nike

我以前使用来避免反复解析服务器响应(如果服务器响应确实如此),那么不会通过计算响应哈希值来更改:

public class HttpClient {

protected OkHttpClient mClient = new OkHttpClient();

public String get(final URL url, final String[] responseHash)
throws IOException {
HttpURLConnection connection = new OkUrlFactory(mClient).open(url);
InputStream inputStream = null;
MessageDigest messageDigest = null;
try {
messageDigest = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
assert messageDigest != null;
try {
// Read the response.
inputStream = connection.getInputStream();
byte[] response = readFully(inputStream);
final byte[] digest = messageDigest.digest(response);
responseHash[0] = Base64.encodeToString(digest, Base64.DEFAULT);
return new String(response, Util.UTF_8);
} finally {
if (inputStream != null) {
inputStream.close();
}
}
}

private byte[] readFully(InputStream in) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
for (int count; (count = in.read(buffer)) != -1; ) {
out.write(buffer, 0, count);
}
return out.toByteArray();
}

}

这是响应头:

HTTP/1.1 200 OK
Server: Apache/2.4.10 (Linux/SUSE)
X-Powered-By: PHP/5.4.20
X-UA-Compatible: IE=edge
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Expires: Thu, 08 Oct 2015 16:15:09 +0000
X-Frame-Options: SAMEORIGIN
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Date: Wed, 07 Oct 2015 16:15:09 GMT
X-Varnish: 505284843
Age: 0
Via: 1.1 varnish
Connection: keep-alive

现在,我切换到 Retrofit,我想知道避免解析相同响应的一种优雅方法是什么? 拦截器是吗?我不负责服务器后端,也无法修改它。

最佳答案

更新

您可以将Expires header 用于缓存控制,以便避免不必要的下载。我认为这不是一个好方法,但是在这种情况下,由于您无法控制服务器端,因此这是我现在想到的唯一方法。

The expiration time of an entity MAY be specified by the origin server using the Expires header (see section 14.21). Alternatively, it MAY be specified using the max-age directive in a response. When the max-age cache-control directive is present in a cached response, the response is stale if its current age is greater than the age value given (in seconds) at the time of a new request for that resource. The max-age directive on a response implies that the response is cacheable (i.e., "public") unless some other, more restrictive cache directive is also present.

If a response includes both an Expires header and a max-age directive, the max-age directive overrides the Expires header, even if the Expires header is more restrictive. This rule allows an origin server to provide, for a given response, a longer expiration time to an HTTP/1.1 (or later) cache than to an HTTP/1.0 cache. This might be useful if certain HTTP/1.0 caches improperly calculate ages or expiration times, perhaps due to desynchronized clocks.

Many HTTP/1.0 cache implementations will treat an Expires value that is less than or equal to the response Date value as being equivalent to the Cache-Control response directive "no-cache". If an HTTP/1.1 cache receives such a response, and the response does not include a Cache-Control header field, it SHOULD consider the response to be non-cacheable in order to retain compatibility with HTTP/1.0 servers.

Note: An origin server might wish to use a relatively new HTTP cache control feature, such as the "private" directive, on a network including older caches that do not understand that feature. The origin server will need to combine the new feature with an Expires field whose value is less than or equal to the Date value. This will prevent older caches from improperly caching the response.



有不同的方法。我用这个:
  • 在服务器响应上,我们获得Etag header 并将其保存在SharedPreferences上。
  • 每个服务器调用都带有带有Etag值的“If-None-Match”头。
  • 服务器比较Etag值并返回304-Not Modified或请求本身的结果(如果更改了某些内容并且需要更新内容)。

  • 如您所指出的,您可以使用 RequestInterceptor来执行此操作:

    public class HeaderRequestInterceptor implements RequestInterceptor {

    private final static String TAG =
    HeaderRequestInterceptor.class.getSimpleName();

    private SharedPreferences mPreferences;

    public HeaderRequestInterceptor() {
    mPreferences = PreferenceManager.getDefaultSharedPreferences(
    DaoApplication.getAppContext());
    }

    @Override
    public void intercept(RequestFacade request) {
    String etagValue = mPreferences.getString(EtagConfig.MY_ETAG_VALUE, "");
    request.addHeader("If-None-Match", etagValue);
    }
    }

    样本输出:
    Retrofit  D  ---> HTTP GET https://url.irontec.com/rest/schedule
    D If-None-Match:
    D Authorization: MyToken M2JiOGQwZGNjNWJiNWNiOTA1Yjc3YTA0YTAyMzEwYWY6OjIwMTUtMTAtMDhUMTM6MDc6MDMrMDA6MDA=
    D Connection: close

    Retrofit D <--- HTTP 200 https://url.irontec.com/rest/schedule (559ms)
    D : HTTP/1.1 200 OK
    D Access-Control-Allow-Credentials: true
    D Access-Control-Allow-Headers: Authorization, Origin, Content-Type, X-CSRF-Token
    D Access-Control-Allow-Methods: GET, PUT, POST, OPTIONS, DELETE
    D Access-Control-Allow-Origin: *
    D Connection: close
    D Content-Type: application/json; charset=UTF-8;
    D Date: Thu, 08 Oct 2015 13:07:07 GMT
    D Etag: a3145c3f85f2dca1c78f87107331c766
    D Server: Apache
    D Transfer-Encoding: chunked
    D X-Android-Received-Millis: 1444309624169
    D X-Android-Response-Source: NETWORK 200
    D X-Android-Sent-Millis: 1444309623870
    D X-Content-Type-Options: nosniff
    D X-Frame-Options: sameorigin

    现在,当刷新内容时:
    Retrofit  D  ---> HTTP GET https://url.irontec.com/rest/schedule
    D If-None-Match: a3145c3f85f2dca1c78f87107331c766
    D Authorization: MyToken MGQ1OWM4YjViYTMxZWM3OGRmMDBlYTZjNmFjNDY3MmI6OjIwMTUtMTAtMDhUMTM6MTA6MDkrMDA6MDA=
    D Connection: close
    D ---> END HTTP (no body)

    Retrofit D <--- HTTP 304 https://url.irontec.com/rest/schedule (299ms)
    D : HTTP/1.1 304 Not Modified
    D Connection: close
    D Date: Thu, 08 Oct 2015 13:10:12 GMT
    D Server: Apache
    D X-Android-Received-Millis: 1444309809335
    D X-Android-Response-Source: NETWORK 304
    D X-Android-Sent-Millis: 1444309809163
    D <--- END HTTP (0-byte body)

    关于http-headers - 当服务器使用Retrofit返回相同的响应时,如何避免解析?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32991921/

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