Skip to content

Instantly share code, notes, and snippets.

@lunaspeed
Last active May 29, 2020 02:05
Show Gist options
  • Save lunaspeed/88954890b682ec4588df003ddc5b9934 to your computer and use it in GitHub Desktop.
Save lunaspeed/88954890b682ec4588df003ddc5b9934 to your computer and use it in GitHub Desktop.
Ignore SSL certificate in Apache HttpClient 4.5.x using Scala
object HttpCreator {
def createHttpClientAcceptsUntrustedCerts(cookieStore: CookieStore): CloseableHttpClient = {
val httpClient: CloseableHttpClient = {
val trustStrategy = new TrustStrategy {
override def isTrusted(x509Certificates: Array[X509Certificate], s: String): Boolean = true
}
val sslContext = new SSLContextBuilder().loadTrustMaterial(null, trustStrategy).build()
val sslSocketFactory = new SSLConnectionSocketFactory(sslContext, new NoopHostnameVerifier)
val socketFactoryRegistry =
RegistryBuilder.create[ConnectionSocketFactory]()
.register("http", PlainConnectionSocketFactory.getSocketFactory)
.register("https", sslSocketFactory)
.build()
val connectionManager = new BasicHttpClientConnectionManager(socketFactoryRegistry)
HttpClients.custom()
.disableRedirectHandling()
.setSSLContext(sslContext)
.setConnectionManager(connectionManager)
.setDefaultCookieStore(cookieStore)
.build()
}
httpClient
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment