Created
April 6, 2017 19:02
-
-
Save NeQuissimus/38865868fe1812f9709f78267f5cb7fa to your computer and use it in GitHub Desktop.
OkHttp client that ignores SSL certificate verification (for testing)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.security.SecureRandom | |
import java.security.cert.{ CertificateException, X509Certificate } | |
import javax.net.ssl.{ HostnameVerifier, SSLContext, SSLSession, X509TrustManager } | |
import scala.util.Try | |
import okhttp3.{ MediaType, OkHttpClient, Request, RequestBody, Response } | |
@SuppressWarnings(Array("org.wartremover.warts.Null")) | |
def getUnsafeClient(): OkHttpClient = { | |
val c = Try { | |
val tm = new X509TrustManager { | |
@throws(classOf[CertificateException]) | |
override def checkClientTrusted(chain: Array[X509Certificate], authType: String): Unit = {} | |
@throws(classOf[CertificateException]) | |
override def checkServerTrusted(chain: Array[X509Certificate], authType: String): Unit = {} | |
override def getAcceptedIssuers(): Array[X509Certificate] = Array[X509Certificate]() | |
} | |
val rand = new SecureRandom() | |
val sslContext = SSLContext.getInstance("SSL") | |
sslContext.init(null, Array(tm), rand) | |
val sslSocketFactory = sslContext.getSocketFactory() | |
val verifier = new HostnameVerifier() { | |
override def verify(hostname: String, session: SSLSession) = true | |
} | |
val client = new OkHttpClient.Builder() | |
val f = client.sslSocketFactory(sslSocketFactory, tm) | |
val v = client.hostnameVerifier(verifier) | |
client.build() | |
} | |
c.getOrElse(new OkHttpClient()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment