Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save cashlalala/b9ba8c83e67eef3b96f7526b1fe12d7f to your computer and use it in GitHub Desktop.
Save cashlalala/b9ba8c83e67eef3b96f7526b1fe12d7f to your computer and use it in GitHub Desktop.
HttpClient 4.5 Ignore SSL Errors and Warnings
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.HttpResponse;
import org.apache.http.client.HttpClient;
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.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLContextBuilder;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
/**
* Code copied from: http://literatejava.com/networks/ignore-ssl-certificate-errors-apache-httpclient-4-4/
*
* This code will not work with OSX JVM 1.6 but does seem to work with Oracle 1.8 on OSX
*/
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 = SSLConnectionSocketFactory.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!
HttpClient client = b.build();
HttpGet get = new HttpGet("https://yoursitehere.com");
//get.setFollowRedirects(true);
HttpResponse response = client.execute(get);
HttpEntity entity = response.getEntity();
//if you got this far, you made it.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment