16

このようなHTTP/2 をサポートするサイトをテストしており、okhttp を使用してリクエストを送信しようとしています。

OkHttpClient okHttpClient = new OkHttpClient();

Request request = new Request.Builder()
        .url("https://www.google.it")
        .build();


okHttpClient.newCall(request).enqueue(new Callback() {
    @Override
    public void onFailure(Request request, IOException e) {
        e.printStackTrace();
    }

    @Override
    public void onResponse(Response response) throws IOException {
        Log.d("TestHttp", "OkHttp-Selected-Protocol: " + response.header("OkHttp-Selected-Protocol"));
        Log.d("TestHttp", "Response code is " + response.code());
    }
});

ログには、次のようなものがあります。

OkHttp-Selected-Protocol: http/1.1

okhttpClient は http/1.1 を使用することを選択しました。HTTP/2 を使用するように強制するにはどうすればよいですか?

4

1 に答える 1

4

Okhttp 2.5+ は、ALPN 経由で 5.0+ を超える http/2 のみをサポートします。

ただし、NPN 経由で 4.0 以上の http/2 をサポートするようにソース コードを変更できます。

于 2015-12-10T03:37:45.937 に答える