如何在 Android 设备上使用 http/2 和 Okhttp?
How to use http/2 with Okhttp on Android devices?
我正在测试支持 HTTP/2、like this 的网站
我尝试使用 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?
Okhttp 2.5+ 仅通过 ALPN 支持 http/2 5.0+ 以上。
但您可以通过 NPN 修改源代码以支持 http/2 4.0+ 以上版本。
我正在测试支持 HTTP/2、like this 的网站 我尝试使用 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?
Okhttp 2.5+ 仅通过 ALPN 支持 http/2 5.0+ 以上。
但您可以通过 NPN 修改源代码以支持 http/2 4.0+ 以上版本。