2

I am using Retrofit 2 with OkHttpClient. I want to make connection having the keep-alive property, so as to make persistent connection with the server which is Https enabled.

Now my problem arises when I am making a network call to Https end point the request follows Http2.0 protocol and the headers Connection : keep-alive are ignored, but when I make the same request to Http end point the headers are accepted and persistent connection works. So to have Http1.1 protocol for Https calls I added manually this to my OkHttpClient.

 OkHttpClient.Builder okhttpclient = new OkHttpClient.Builder();
 okhttpclient.protocols(Arrays.asList(Protocol.HTTP_1_1));

But still the OkHttpClient sends a Http2.0 request to server for Https network calls.

I even tried to keep persistent connection on user end by using the ConnectionPool class, but didn't find that as a good solution.

I also tried adding a sslSocketFactory to my OkHttpClient for Https requests. That too didn't work out.

How should I force the Http1.1 protocol for my Https requests with OkHttpClient?

saurabhlahoti
  • 466
  • 8
  • 21

1 Answers1

0

You’re correctly forcing OkHttp to use HTTP/1.1 with this line:

okhttpclient.protocols(Arrays.asList(Protocol.HTTP_1_1));

Are you certain that the OkHttpClient produced by that builder is the one you’re using with your Retrofit instance?

Jesse Wilson
  • 39,078
  • 8
  • 121
  • 128
  • Yes. My retrofit instance is singleton. The same request when sent to Http follows Http1.1 but when sending to Https follows Http2.0 on its own even after forcibly specifying the Http1.1 protocol. – saurabhlahoti Apr 15 '17 at 17:37
  • This is surprising. If you can reproduce the problem in a small test case, please do and then report a bug against the OkHttp issue tracker. – Jesse Wilson Apr 17 '17 at 01:59