OkHttp3 In-None-Match not sent



我正在使用0KHTTP 3.6.0
第一请求的回应:

05-05 04:54:56.524 W/genymotion_audio: out_write() limiting sleep time 53061 to 39909
05-05 04:54:56.624 D/OkHttp: <-- 200 OK https://example.com/api/v2/products/12?include=variants,brand,option_types.option_values,product_articles (296ms)
05-05 04:54:56.624 D/OkHttp: Connection: close
05-05 04:54:56.624 D/OkHttp: Content-Type: application/json; charset=utf-8
05-05 04:54:56.624 D/OkHttp: Server: Cowboy
05-05 04:54:56.624 D/OkHttp: Date: Fri, 05 May 2017 08:54:59 GMT
05-05 04:54:56.624 D/OkHttp: X-Frame-Options: SAMEORIGIN
05-05 04:54:56.624 D/OkHttp: X-Xss-Protection: 1; mode=block
05-05 04:54:56.624 D/OkHttp: X-Content-Type-Options: nosniff
05-05 04:54:56.624 D/OkHttp: Vary: User-Agent
05-05 04:54:56.624 D/OkHttp: Etag: "75587e81446d816b70a25dbfc602bcab"
05-05 04:54:56.624 D/OkHttp: Last-Modified: Tue, 03 Jan 2017 05:37:37 GMT
05-05 04:54:56.624 D/OkHttp: Access-Control-Allow-Origin: *
05-05 04:54:56.624 D/OkHttp: Access-Control-Allow-Methods: POST, GET, PUT, DELETE, OPTIONS
05-05 04:54:56.624 D/OkHttp: Access-Control-Max-Age: 1728000
05-05 04:54:56.624 D/OkHttp: Access-Control-Allow-Headers: Authorization,Content-Type
05-05 04:54:56.624 D/OkHttp: Cache-Control: max-age=0, private, must-revalidate
05-05 04:54:56.624 D/OkHttp: X-Request-Id: e6a2bdfc-666f-499e-8a4c-07b61199fa78
05-05 04:54:56.624 D/OkHttp: X-Runtime: 0.042622
05-05 04:54:56.624 D/OkHttp: Via: 1.1 vegur

第二请求:

05-05 04:56:38.652 D/OkHttp: --> GET https://example.com/api/v2/products/12?include=variants,brand,option_types.option_values,product_articles http/1.1
05-05 04:56:38.652 D/OkHttp: X-OS-Name: android
05-05 04:56:38.652 D/OkHttp: X-Platform: mobile_app
05-05 04:56:38.652 D/OkHttp: X-Device-Model: Genymotion Samsung Galaxy S5 - 4.4.4 - API 19 - 1080x1920
05-05 04:56:38.652 D/OkHttp: X-OS-Version: 4.4.4
05-05 04:56:38.652 D/OkHttp: Accept-Language: en-SG
05-05 04:56:38.652 D/OkHttp: --> END GET  

okhttp客户端:

    private static Interceptor headerInterceptor = new Interceptor() {
        @Override
        public Response intercept(Chain chain) throws IOException {
            Request.Builder builder = chain.request().newBuilder();
            builder.addHeader("X-OS-Name", "android");
            builder.addHeader("X-Platform", "mobile_app");
            builder.addHeader("X-Device-Model", DeviceUtils.getDeviceModel());
            builder.addHeader("X-OS-Version", DeviceUtils.getDeviceVersion());
            Request request = builder.build();
            return chain.proceed(request);
        }
    };
private static OkHttpClient getOkHttpClient(Context context) {
        OkHttpClient.Builder builder = new OkHttpClient.Builder()
                .addInterceptor(headerInterceptor)
                .addInterceptor(loggingInterceptor)
                .connectTimeout(20, TimeUnit.SECONDS)
                .writeTimeout(20, TimeUnit.SECONDS)
                .readTimeout(60, TimeUnit.SECONDS);
        int cacheSize = 10 * 1024 * 1024; // 10 MiB
        File cacheDir = new File(context.getCacheDir(), "http");
        Cache cache = new Cache(cacheDir, cacheSize);
        builder.cache(cache);
        return builder.build();
    }

我已经阅读了
我不想在本地数据存储中手动存储ETAG值并将其附加在匹配请求中,因为这会阻碍性能。

它已经在起作用,问题是我使用的记录插头没有显示http 304
(addInterceptor位于缓存和应用程序之间,如下所示(

addInterceptor更改为addNetworkInterceptor揭示Okhttp的魔术:

05-05 05:21:33.436 D/OkHttp: --> GET https://example.com/api/v2/products/12?include=variants,brand,option_types.option_values,product_articles http/1.1
05-05 05:21:33.436 D/OkHttp: X-OS-Name: android
05-05 05:21:33.436 D/OkHttp: X-Platform: mobile_app
05-05 05:21:33.436 D/OkHttp: X-Device-Model: Genymotion Samsung Galaxy S5 - 4.4.4 - API 19 - 1080x1920
05-05 05:21:33.436 D/OkHttp: X-OS-Version: 4.4.4
05-05 05:21:33.436 D/OkHttp: Accept-Language: en-SG
05-05 05:21:33.436 D/OkHttp: Host: example.herokuapp.com
05-05 05:21:33.436 D/OkHttp: Connection: Keep-Alive
05-05 05:21:33.436 D/OkHttp: Accept-Encoding: gzip
05-05 05:21:33.436 D/OkHttp: User-Agent: okhttp/3.6.0
05-05 05:21:33.436 D/OkHttp: If-None-Match: "75587e81446d816b70a25dbfc602bcab"
05-05 05:21:33.436 D/OkHttp: --> END GET
05-05 05:21:33.892 D/OkHttp: <-- 304 Not Modified https://example.com/api/v2/products/12?include=variants,brand,option_types.option_values,product_articles (455ms)
05-05 05:21:33.892 D/OkHttp: Server: Cowboy
05-05 05:21:33.892 D/OkHttp: Content-Length: 0
05-05 05:21:33.892 D/OkHttp: Connection: keep-alive
05-05 05:21:33.892 D/OkHttp: Date: Fri, 05 May 2017 09:23:14 GMT
05-05 05:21:33.892 D/OkHttp: X-Frame-Options: SAMEORIGIN
05-05 05:21:33.892 D/OkHttp: X-Xss-Protection: 1; mode=block
05-05 05:21:33.892 D/OkHttp: X-Content-Type-Options: nosniff
05-05 05:21:33.892 D/OkHttp: Vary: User-Agent
05-05 05:21:33.892 D/OkHttp: Etag: "75587e81446d816b70a25dbfc602bcab"
05-05 05:21:33.892 D/OkHttp: Last-Modified: Tue, 03 Jan 2017 05:37:37 GMT
05-05 05:21:33.892 D/OkHttp: Access-Control-Allow-Origin: *
05-05 05:21:33.892 D/OkHttp: Access-Control-Allow-Methods: POST, GET, PUT, DELETE, OPTIONS
05-05 05:21:33.892 D/OkHttp: Access-Control-Max-Age: 1728000
05-05 05:21:33.892 D/OkHttp: Access-Control-Allow-Headers: Authorization,Content-Type
05-05 05:21:33.892 D/OkHttp: Cache-Control: max-age=0, private, must-revalidate
05-05 05:21:33.892 D/OkHttp: X-Request-Id: 941c04d1-2d5c-44b7-b6ef-82a455d95dc1
05-05 05:21:33.892 D/OkHttp: X-Runtime: 0.053531
05-05 05:21:33.892 D/OkHttp: Via: 1.1 vegur
05-05 05:21:33.892 D/OkHttp: <-- END HTTP (0-byte body)

代码:

private static OkHttpClient getOkHttpClient(Context context) {
        OkHttpClient.Builder builder = new OkHttpClient.Builder()
                .addInterceptor(headerInterceptor)
                .addNetworkInterceptor(loggingInterceptor)
                .connectTimeout(20, TimeUnit.SECONDS)
                .writeTimeout(20, TimeUnit.SECONDS)
                .readTimeout(60, TimeUnit.SECONDS);
        int cacheSize = 10 * 1024 * 1024; // 10 MiB
        File cacheDir = new File(context.getCacheDir(), "http");
        Cache cache = new Cache(cacheDir, cacheSize);
        builder.cache(cache);
        return builder.build();
    }

相关内容

最新更新