Skip to content

HttpClient 4.5 Ignore SSL Errors and Warnings

allesgutevn edited this page Apr 11, 2018 · 1 revision

import java.security.cert.CertificateException; import java.security.cert.X509Certificate;

import javax.net.ssl.HostnameVerifier; import javax.net.ssl.SSLContext;

import org.apache.http.HttpEntity; import org.apache.http.HttpHost; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.config.Registry; import org.apache.http.config.RegistryBuilder; import org.apache.http.conn.socket.ConnectionSocketFactory; import org.apache.http.conn.socket.PlainConnectionSocketFactory; import org.apache.http.conn.ssl.NoopHostnameVerifier; import org.apache.http.conn.ssl.SSLConnectionSocketFactory; import org.apache.http.conn.ssl.SSLContextBuilder; import org.apache.http.conn.ssl.SSLSocketFactory; import org.apache.http.conn.ssl.TrustStrategy; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; import org.apache.http.util.EntityUtils;

/**

*/

public class TestHttpClient45IgnoreSSLErrors {

public static void main(String[] args) throws Exception {

HttpClientBuilder b = HttpClientBuilder.create();

// setup a Trust Strategy that allows all certificates. // SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy()

{

public boolean isTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {

return true;

}

}).build();

b.setSslcontext(sslContext);

// don't check Hostnames, either. // -- use SSLConnectionSocketFactory.getDefaultHostnameVerifier(), if you don't want to weaken HostnameVerifier hostnameVerifier = SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;

// here's the special part: // -- need to create an SSL Socket Factory, to use our weakened "trust strategy"; // -- and create a Registry, to register it. // SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext, hostnameVerifier); Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory> create()

.register("http", PlainConnectionSocketFactory.getSocketFactory()).register("https", sslSocketFactory).build();

// now, we create connection-manager using our Registry. // -- allows multi-threaded use PoolingHttpClientConnectionManager connMgr = new PoolingHttpClientConnectionManager(socketFactoryRegistry); b.setConnectionManager(connMgr);

// finally, build the HttpClient; // -- done! CloseableHttpClient client = b.setProxy(new HttpHost("proxy.domain.net", 3128)).build();

HttpGet get = new HttpGet("https://input.livetracking.io/time"); //get.setFollowRedirects(true);

CloseableHttpResponse response = client.execute(get);

try {
HttpEntity entity = response.getEntity(); System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); System.out.println(EntityUtils.toString(entity));
} finally {
response.close();

}

//if you got this far, you made it.

}

}

test sidebar

Clone this wiki locally