when I hit API form POSTMAN, I am able to get the response. But when I hit using code, "Android app using Retrofit", I am getting following error: "java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.".
I had tried below options to resolve the issue.
- manifest : android:usesCleartextTraffic="true" & android:networkSecurityConfig="@xml/network_security_config" 2.network_security_config.xml
<network-security-config> xmlns:android="http://schemas.android.com/apk/res/android">
<base-config cleartextTrafficPermitted="true" />
</network-security-config>
using certificate: taken ref from internet: object HttpClient { // Create a trust manager that does not validate certificate chains val unsafeOkHttpClient: OkHttpClient
// Install the all-trusting trust manager
// Create an ssl socket factory with our all-trusting manager get() = try { // Create a trust manager that does not validate certificate chains val trustAllCerts: Array<TrustManager> = arrayOf<TrustManager>( object : X509TrustManager { @Throws(CertificateException::class) override fun checkClientTrusted( chain: Array<X509Certificate?>?, authType: String? ) { } @Throws(CertificateException::class) override fun checkServerTrusted( chain: Array<X509Certificate?>?, authType: String? ) { } override fun getAcceptedIssuers(): Array<X509Certificate?>? { return arrayOf() } /* override fun getAcceptedIssuers(): Array<X509Certificate> { TODO("Not yet implemented") }*/ /* val acceptedIssuers: Array<X509Certificate> get() = arrayOf(_AcceptedIssuers)*/ } ) // Install the all-trusting trust manager val sslContext: SSLContext = SSLContext.getInstance("SSL") sslContext.init(null, trustAllCerts, SecureRandom()) // Create an ssl socket factory with our all-trusting manager val sslSocketFactory: SSLSocketFactory = sslContext.getSocketFactory() val builder = OkHttpClient.Builder() builder.sslSocketFactory(sslSocketFactory, trustAllCerts[0] as X509TrustManager) builder.hostnameVerifier(object : HostnameVerifier { override fun verify(hostname: String?, session: SSLSession?): Boolean { return true } }) .connectTimeout(1, TimeUnit.MINUTES) .readTimeout(30, TimeUnit.SECONDS) .writeTimeout(15, TimeUnit.SECONDS) builder.build() } catch (e: Exception) { throw RuntimeException(e) }
} I am able to hit the API and get the response. Can someone tell me why Server is asking for Certificate while hitting the API from code?
Thanks in advance.