Last active
May 29, 2020 02:05
-
-
Save lunaspeed/88954890b682ec4588df003ddc5b9934 to your computer and use it in GitHub Desktop.
Ignore SSL certificate in Apache HttpClient 4.5.x using Scala
This file contains hidden or 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
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