Reputation: 2008
I am using OkHTTP client to access HTTP/2 server. I have a use case where I want to open multiple streams over same connection.
I am using following code to create OkHTTP client.
ConnectionPool connectionPool = new ConnectionPool(5,
CONNECTION_POOL_TIMEOUT_MILLISECONDS, TimeUnit.MILLISECONDS);
return new OkHttpClient.Builder()
.connectTimeout(5,TimeUnit.SECONDS)
.writeTimeout(5, TimeUnit.SECONDS)
.readTimeout(5, TimeUnit.SECONDS)
.connectionPool(connectionPool)
.build();
This client can have 5 open connection in the connection pool. Now I want to make following requests using above client,
okHttpClient.newCall(request1.build()).execute()
okHttpClient.newCall(request2.build()).execute()
And I want these requests as a separate stream over the same connection, instead of a new connection. How can I do that?
Upvotes: 0
Views: 972
Reputation: 109
OkHttp will use the same connection for the unique server address. And the requests send over that connection will be consumed in a new stream every time.
Upvotes: 0
Reputation: 40593
OkHttp will automatically use the same socket for your requests if they’re eligible. The requests need to be HTTPS, the server needs to support HTTP/2, and your code must run on Android 5+ or Java 9+.
Upvotes: 1