Skip to content

Instantly share code, notes, and snippets.

@helmbold
Last active August 3, 2018 03:50
Show Gist options
  • Save helmbold/c7808b17bcf5a5d009cf to your computer and use it in GitHub Desktop.
Save helmbold/c7808b17bcf5a5d009cf to your computer and use it in GitHub Desktop.
Apache HTTP Client (version 4.5) that ignores certificate warnings/errors
import java.security.cert.X509Certificate
import org.apache.http.config.RegistryBuilder
import org.apache.http.conn.socket.{ConnectionSocketFactory, PlainConnectionSocketFactory}
import org.apache.http.conn.ssl.{NoopHostnameVerifier, SSLConnectionSocketFactory, TrustStrategy}
import org.apache.http.impl.client.{CloseableHttpClient, HttpClients}
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager
import org.apache.http.ssl.SSLContextBuilder
object CarelessHttpsClient {
val instance: CloseableHttpClient = {
val trustStrategy = new TrustStrategy {
override def isTrusted(x509Certificates: Array[X509Certificate], s: String) = 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 PoolingHttpClientConnectionManager(socketFactoryRegistry)
HttpClients.custom()
.setSSLContext(sslContext)
.setConnectionManager(connectionManager)
.build()
}
}
Copy link

ghost commented Jan 26, 2018

Thank you! :)

@bmogensen
Copy link

Thank you, this worked great!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment