I'd like to make a request and force it to use the Protocol.HTTP_2. I tried the code below:
import okhttp3.{OkHttpClient, Protocol, Request}
import scala.collection.JavaConversions._
import scala.collection.mutable.ListBuffer
object Main2 extends App {
val url = "https://google.com/"
val client = new OkHttpClient.Builder().protocols(ListBuffer(Protocol.HTTP_2)).build()
val request = new Request.Builder().url(url).build()
val response = client.newCall(request).execute()
println(response.body().string())
}
But a got the error: Exception in thread "main" java.lang.IllegalArgumentException: protocols doesn't contain http/1.1: [h2]
OkHttp will automatically use HTTP/2 if it’s available, but you can’t disable HTTP/1.1.
If you aren't using SSL/TLS you can use Protocol.H2_PRIOR_KNOWLEDGE
instead of Protocol.HTTP_2
when creating the OkHttpClient.
This will send a cleartext HTTP/2 that doesn't first negotiate with the service on which protocol to use.
For me, this forces HTTP/2 when the server is multiplexing HTTP/1.1 and HTTP/2.
Source: https://square.github.io/okhttp/3.x/okhttp/okhttp3/Protocol.html#H2_PRIOR_KNOWLEDGE
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With