【问题标题】:Okhttp not selecting http2 on Android 7.0Okhttp 在 Android 7.0 上没有选择 http2
【发布时间】:2017-10-22 16:43:11
【问题描述】:

我正在尝试在运行 Android 7.0 的设备上向以下域(启用 http2)发出 HTTP 请求。

我使用的代码如下:

Request request = new Request.Builder()
                .url("https://http2.akamai.com/")
                .build();

response = okHttpClient.newCall(request).execute();
statusCode = response.code();

我在响应对象中注意到,okHttp 使用的协议是HTTP 1.1

okhttp 客户端的行为是随机的,有一次我可以看到协议为h2,但随后重复请求,它一直选择HTTP 1.1

我正在使用 okhttp v3.5

我可能在这里错过了什么?

【问题讨论】:

    标签: android okhttp3 http2 okhttp


    【解决方案1】:

    Android 在其 OkHttp 中仅启用了 HTTP/1.1。

    /**
     * Creates an OkHttpClient suitable for creating HttpsURLConnection instances on
     * Android.
     */
    public static OkUrlFactory createHttpsOkUrlFactory(Proxy proxy) {
      ...
      // Only enable HTTP/1.1 (implies HTTP/1.0). Disable SPDY / HTTP/2.0.
      okHttpClient.setProtocols(HTTP_1_1_ONLY);
    

    https://android.googlesource.com/platform/external/okhttp/+/master/android/main/java/com/squareup/okhttp/HttpsHandler.java#75

    【讨论】:

      猜你喜欢
      • 2016-08-12
      • 2019-10-15
      • 2019-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-21
      • 2017-06-27
      相关资源
      最近更新 更多