0

Kotlin Retrofit ignore https certifcate.

I tried this Android ignore self signed certificate and converted to Kotlin. Its not working.

How can i ignore https (SSL) certificate.

My OkHttp is


import com.jakewharton.retrofit2.adapter.kotlin.coroutines.CoroutineCallAdapterFactory
import okhttp3.Interceptor
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit

object OwnRetrofitClient {

    private val authInterceptor = Interceptor { chain ->
        val newUrl = chain.request().url
            .newBuilder()
            .build()

        val newRequest = chain.request()
            .newBuilder()
            .addHeader("Authorization", "bearer " + AppPreferences.token)
            .url(newUrl)
            .build()

        chain.proceed(newRequest)
    }

    private val client =
        OkHttpClient().newBuilder()
            .addInterceptor(authInterceptor)
            .connectTimeout(120, TimeUnit.SECONDS)
            .readTimeout(120, TimeUnit.SECONDS)
            .writeTimeout(90, TimeUnit.SECONDS)
            .build()

    private fun retrofit(baseUrl: String = "https:some.com/api") =
        Retrofit.Builder()
            .client(client)
            .baseUrl(baseUrl)
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(CoroutineCallAdapterFactory())
            .build()

    fun apiService(): ApiService {
        return retrofit().create(ApiService::class.java)
    }
}```


tomerpacific
  • 4,704
  • 13
  • 34
  • 52
doseeare
  • 1
  • 1

1 Answers1

0

The example you refer to, uses java built-in HttpsUrlConnection. In okhttp, you need to add the X509TrustManager, the HostnameVerifier and the SSLSocketFactory to OkHttpClient.Builder (see https://square.github.io/okhttp/4.x/okhttp/okhttp3/-ok-http-client/-builder/ssl-socket-factory/)

J.Gerbershagen
  • 316
  • 1
  • 3