HttpComponents Custom SSLSocketFactory

ItinerantEngineer picture ItinerantEngineer · Aug 21, 2012 · Viewed 7.4k times · Source

Per the example at http://hc.apache.org/httpcomponents-client-ga/httpclient/examples/org/apache/http/examples/client/ClientCustomSSL.java

SSLSocketFactory socketFactory = new SSLSocketFactory(trustStore);

Should instantiate a SSLSocketFactory using my custom trust store, yet when I try to build my project, I get the error

javax.net.ssl.SSLSocketFactory is abstract; cannot be intantiated

Is the example out of date? Does it work for other people?

Answer

Bruno picture Bruno · Aug 21, 2012

It's a org.apache.http.conn.ssl.SSLSocketFactory you should use, not a javax.net.ssl.SSLSocketFactory (see import directive in the example file you're using).

Note that the former has constructors that can use the latter if you already have existing code for this. However, you would generally get a custom javax.net.ssl.SSLSocketFactory from an SSLContext, so you might as well use the SSLContext-based constructor in this case.