Java okhttp3 only uses http / 1.1 or http / 2
•
Java
Try to execute some test cases for HTTP requests I want to distinguish between http / 1.1 and HTTP / 2.0 To do this, I need to know which version I used from the request I also want to force requests to use HTTP / 1.1 or http / 2 I'm using okhttp3 for Java
Request request = new Request.Builder() .url(url) .build(); Response response = client.newCall(request).execute(); return response.body().string();
Solution
You can force the request to be http / 1.1 using the following code
new OkHttpClient.Builder().protocols(Arrays.asList(Protocol.HTTP_1_1));
You cannot force http / 2 because http / 1.1 must be in the protocols list
However, you can confirm this by checking the protocol on the response object
Request request = new Request.Builder() .url("http://publicobject.com/helloworld.txt") .build(); Response response = client.newCall(request).execute(); assert(response.protocol() == Protocol.HTTP_2);
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码