I work with Retrofit2, when I try to access the https protocol, it starts swearing, how can I connect a certificate to it? On the Internet, the code found something like:

OkHttpClient okHttp = new OkHttpClient(); okHttp.setSslSocketFactory(getSSLConfig(contex).getSocketFactory()); Retrofit retrofit = builder.client(okHttp).build(); retrofit.create(serviceClass) 

I connected OkHttp but ... setSslSocketFactory did not find the setSslSocketFactory method there.

PS Connected OkHttp from:

compile 'com.squareup.okhttp3: okhttp: 3.6.0'

Sources:

  Retrofit.Builder builder = new Retrofit.Builder() .baseUrl("https://site.cf") .addConverterFactory(GsonConverterFactory.create(gson)); CertificatePinner certificatePinner = new CertificatePinner.Builder().add("site.cf","sha256/key").build(); OkHttpClient client = new OkHttpClient.Builder() .certificatePinner(certificatePinner) .build(); Retrofit retrofit = builder.client(client).build(); API api = retrofit.create(API.class); 
  • I added source codes, having connected the certificate I still receive javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found. - Heaven
  • And the certificate is not self-signed by chance? - rjhdby
  • With self-signed certificates this does not work? - Heaven

1 answer 1

look in the direction of CustomTrust something there (I am writing from the phone, it is difficult to search :) in the okhttp3 library. And google how to use it for selfsigned

PS damn, how the answer post it ...

  • If the certificate is confirmed, will the code that I wrote above in the source code work? - Heaven
  • Or does it matter who confirmed the certificate? - Heaven
  • The signing of the certificate, alas, did not help - Heaven
  • You were right, having run CustomTrust stumbled upon an article with the implementation of a method that helped stackoverflow.com/questions/27716001 / - Heaven