Retrofit/Okhttp Https is compatible with the Http downgrade processing method, retrofitokhttp
Trust all https
Trust all https requests, that is, allow all certificates for verification.OkHttpClientSet HostnameVerifier to true
OkHttpClient client = new OkHttpClient (); client. setHostnameVerifier (new HostnameVerifier () {@ Override public boolean verify (String s, SSLSession sslSession) {return true ;}}); trustManager [] trustAllCerts = new TrustManager [] {new X509TrustManager () {@ Override public void checkClientTrusted (java. security. cert. x509Certificate [] x509Certificates, String s) throws java. security. cert. certificateException {}@ Override public void checkServerTrusted (java. security. cert. x509Certificate [] x509Certificates, String s) throws java. security. cert. certificateException {}@ Override public java. security. cert. x509Certificate [] getAcceptedIssuers () {return new java. security. cert. x509Certificate [] {}}}}; try {// construct your own SSLContext SC = SSLContext. getInstance ("TLS"); SC. init (null, trustAllCerts, new java. security. secureRandom (); client. setSslSocketFactory (SC. getSocketFactory ();} catch (Exception e) {e. printStackTrace ();}
Set the Protocol to 1.1 (Protocol. HTTP_1_1)
client.protocols(Collections.singletonList(Protocol.HTTP_1_1)) .build();
Currently, OKhttp3.5 has put the setHostnameVerifier function into the builder. You can use OkHttpClientbuilder. HostnameVerifier to perform operations. In fact, all internal calls are the same.
Derivative
Currently, many frameworks use okhttp, and the famous ones include picasso, glide, and Retrofit. Through the above posture, these frameworks can support Https connections by default.
Today's article is short, but its practicality is relatively high.