Last active
August 3, 2018 03:50
-
-
Save helmbold/c7808b17bcf5a5d009cf to your computer and use it in GitHub Desktop.
Apache HTTP Client (version 4.5) that ignores certificate warnings/errors
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.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() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you! :)