I am trying to **accept all certificates, and/or accept self-signed certificates** using **Apache HTTPClient version 4.5** (tutorial link [here](http://hc.apache.org/httpcomponents-client-ga/tutorial/html/index.html))

I've been going through solutions to this problem from a bunch of posts on SO. So far none of them have worked. 

I keep getting this error: **`Error while trying to execute request. javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake`**

***Apache Docs:***

* [Apache v4.5 tutorial](http://hc.apache.org/httpcomponents-client-ga/tutorial/html/index.html)
* [SSL/TLS customization](http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html#d5e449)
* [Apache has a guide](http://hc.apache.org/httpclient-3.x/sslguide.html) for version 3, but not version 4.

***Related StackOverflow Questions*** - Here's some links of the solutions I've tried:

* [Ignoring SSL certificate in Apache HttpClient 4.3](https://stackoverflow.com/questions/19517538/ignoring-ssl-certificate-in-apache-httpclient-4-3)
* [How to ignore SSL certificate errors in Apache HttpClient 4.0](https://stackoverflow.com/questions/2703161/how-to-ignore-ssl-certificate-errors-in-apache-httpclient-4-0)
* [Ignore SSL Certificate Errors with Java](https://stackoverflow.com/questions/12060250/ignore-ssl-certificate-errors-with-java)
* [Need to trust all the certificates during the development using Spring](https://stackoverflow.com/questions/6789597/need-to-trust-all-the-certificates-during-the-development-using-spring)
* [How to handle invalid SSL certificates with Apache HttpClient?](https://stackoverflow.com/questions/1828775/how-to-handle-invalid-ssl-certificates-with-apache-httpclient)

Note that in all these examples I am also passing a cookie store and a proxy credentials provider that I defined earlier. These are working, I'm just trying to add SSL support.

Try #1
======

Create my own ssl context with `SSLContextBuilder` and trust all self signed strategies with `TrustSelfSignedStrategy`. 

```
SSLContextBuilder sshbuilder = new SSLContextBuilder();
sshbuilder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sshbuilder.build());

CloseableHttpClient httpclient = HttpClients.custom()
    .setDefaultCredentialsProvider(credsProvider)
    .setDefaultCookieStore(cookieStore)
    .setSSLSocketFactory(sslsf)
    .build();

```

**RESULT**: Didn't work. Got `Error while trying to execute request. javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake`

Try #2
======

Same as above, but add a `PoolingHttpClientConnectionManager` 

```
SSLContextBuilder builder = new SSLContextBuilder();
builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(builder.build(),SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
        .register("http", new PlainConnectionSocketFactory())
        .register("https", sslsf)
        .build();
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(registry);
cm.setMaxTotal(2000);//max connection

CloseableHttpClient httpclient = HttpClients.custom()
    .setDefaultCredentialsProvider(credsProvider)
    .setDefaultCookieStore(cookieStore)
    .setSSLSocketFactory(sslsf)
    .setConnectionManager(cm)
    .build();

```

**RESULT**: Didn't work. Got `Error while trying to execute request. javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake`

Try #3
======

Simply accept ALL certificates by overriding the `TrustStrategy` (this is not recommended)

```
SSLContextBuilder builder = new SSLContextBuilder();
builder.loadTrustMaterial(null, new TrustStrategy() {
    @Override
    public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
        return true;
    }
});
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(builder.build(),
    SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

CloseableHttpClient httpclient = HttpClients.custom()
    .setDefaultCredentialsProvider(credsProvider)
    .setDefaultCookieStore(cookieStore)
    .setSSLSocketFactory(sslsf)
    .build();

```

**RESULT**: Didn't work. Got `Error while trying to execute request. javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake`

Try #4
======

I found something useful from [this answer](https://stackoverflow.com/a/34654349/1696153): 

> 
> As of version 4.5 HttpClient disables SSLv3 protocol version by
>  default
> 
> 
> 

Here's the solution he gave:

```
SSLContext sslcontext = SSLContexts.createSystemDefault();
SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(
        sslcontext, new String[] { "TLSv1", "SSLv3" }, null,
        SSLConnectionSocketFactory.getDefaultHostnameVerifier());

Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
        .register("http", PlainConnectionSocketFactory.INSTANCE)
        .register("https", sslConnectionSocketFactory)
        .build();
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(socketFactoryRegistry);

httpclient = HttpClients.custom()
    .setDefaultCredentialsProvider(credsProvider)
    .setDefaultCookieStore(cookieStore)
    .setSSLSocketFactory(sslConnectionSocketFactory)
    .setConnectionManager(cm)
    .build();

```

**RESULT**: Didn't work. Got `Error while trying to execute request. javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake`