[英]Java OkHttp3 only use Http/1.1 OR Http/2
Trying to do some test-cases for HTTP requests. 尝试为HTTP请求执行一些测试用例。 I want to distinguish between HTTP/1.1 and HTTP/2.
我想区分HTTP / 1.1和HTTP / 2。 In order to do that, I need to know which version I have used from a request.
为了做到这一点,我需要知道我从请求中使用了哪个版本。 I also want to force the request to use HTTP/1.1 or HTTP/2.
我还想强制请求使用HTTP / 1.1或HTTP / 2。 I am using OkHttp3 for Java.
我正在使用OkHttp3 for Java。 Here is a simple request which I do:
这是一个简单的请求,我这样做:
Request request = new Request.Builder()
.url(url)
.build();
Response response = client.newCall(request).execute();
return response.body().string();
You can force the request to be HTTP/1.1 only with the following code 您可以使用以下代码强制请求为HTTP / 1.1
new OkHttpClient.Builder().protocols(Arrays.asList(Protocol.HTTP_1_1));
You can't force HTTP/2 only as HTTP/1.1 must be in the protocols list. 您不能仅强制HTTP / 2,因为HTTP / 1.1必须在协议列表中。
However you can confirm by checking protocol on the Response object 但是,您可以通过检查Response对象上的协议来确认
Request request = new Request.Builder()
.url("http://publicobject.com/helloworld.txt")
.build();
Response response = client.newCall(request).execute();
assert(response.protocol() == Protocol.HTTP_2);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.