title: Error when trusting HTTPS certificate using okhttp in Kotlin
body: <p>I have found many related questions on this and I have tried many options. But I'm not able to resolve it. I want to disable certification checking on making an API call using <code>okhttp</code>. </p>
  
  <p><strong>Below is the code that I tried</strong></p>
  
  <pre><code>import okhttp3.OkHttpClient
  import java.security.cert.CertificateException
  import javax.net.ssl.SSLContext
  import javax.net.ssl.TrustManager
  import javax.net.ssl.X509TrustManager
  
      class UnsafeOkHttpClient {
      companion object {
          fun getUnsafeOkHttpClient(): OkHttpClient.Builder {
              try {
                  // Create a trust manager that does not validate certificate chains
                  val trustAllCerts = arrayOf&lt;TrustManager&gt;(object : X509TrustManager {
                      @Throws(CertificateException::class)
                      override fun checkClientTrusted(chain: Array&lt;java.security.cert.X509Certificate&gt;, authType: String) {
                      }
  
                      @Throws(CertificateException::class)
                      override fun checkServerTrusted(chain: Array&lt;java.security.cert.X509Certificate&gt;, authType: String) {
                      }
  
                      override fun getAcceptedIssuers(): Array&lt;java.security.cert.X509Certificate&gt; {
                          return arrayOf()
                      }
                  })
  
                  // Install the all-trusting trust manager
                  val sslContext = SSLContext.getInstance("SSL")
                  sslContext.init(null, trustAllCerts, java.security.SecureRandom())
                  // Create an ssl socket factory with our all-trusting manager
                  val sslSocketFactory = sslContext.socketFactory
  
                  val builder = OkHttpClient.Builder()
                  builder.sslSocketFactory(sslSocketFactory, trustAllCerts[0] as X509TrustManager)
                  builder.hostnameVerifier { _, _ -&gt; true }
  
                  return builder
              } catch (e: Exception) {
                  throw RuntimeException(e)
              }
          }
      }
  }
  </code></pre>
  
  <p><strong>On making request</strong>:</p>
  
  <pre><code> fun getAppList(requestbody: AppListRequestBody, baseUrl: String): AppListResponse {
  
          val gson = GsonBuilder().create()
          val appListRequestInfo: String = gson.toJson(requestbody)
          val JSON = MediaType.parse("application/json")
          val appListBody: RequestBody = RequestBody.create(JSON, appListRequestInfo)
          val client =getUnsafeOkHttpClient().build
  
          var appListRequest: Request = Request.Builder()
                  .url("$baseUrl/apps/mobile")
                  .post(appListBody)
                  .addHeader("Content-Type", "application/json")
                  .build()
          val appResponse: Response = client.newCall(appListRequest).execute() //Getting syntax error
          if (!appResponse.isSuccessful) {
              Log.e("Exception =",appResponse.message())
              throw Exception(appResponse.code().toString())
          }
          Log.d("App list res",appResponse.body().toString())
          return Gson().fromJson(appResponse.body()!!.string(), AppListResponse::class.java)
      }
  </code></pre>
  
  <p><strong>I'm getting an exception:</strong></p>
  
  <pre><code>Response{protocol=http/1.1, code=404, message=Not Found}
  </code></pre>
  
  <p>Please help me with this.Also, why does it show protocol as http when the url is https? Is there any ways to set that. Please help me resolve this issue.</p>