How to use http/2 with Okhttp on Android devices?

泪湿孤枕 提交于 2020-01-01 08:30:09

问题


I'M testing a site that supports HTTP/2,like this, and I try to use okhttp to send the request:

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());
    }
});

In the log I got something like this:

OkHttp-Selected-Protocol: http/1.1

The okhttpClient chose to use http/1.1, how can I force it to use HTTP/2?


回答1:


Okhttp 2.5+ only support http/2 above 5.0+ via ALPN.

but you can modify the source code to support http/2 above 4.0+ via NPN.



来源:https://stackoverflow.com/questions/31850249/how-to-use-http-2-with-okhttp-on-android-devices

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!