Self signed certificates in Apache HttpClient
When you need to support self-signed SSL certificates in your Apache HttpClient based application you can use the contributed EasySSLProtocolSocketFactory as described in the HttpClient docs. Instead of using HttpClient’s HostConfiguration object directly you’d modify its protocol socket factory in your code like so:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | ... if (config.isAllowSelfSignedCertificates()) { ProtocolSocketFactory factory = new EasySSLProtocolSocketFactory(); try { URI uri = new URI(config.getBaseUrl()); int port = uri.getPort(); if (port == -1) { port = 443; } Protocol easyHttps = new Protocol(uri.getScheme(), factory, port); hostConfiguration.setHost(uri.getHost(), port, easyHttps); } catch (URISyntaxException e) { throw new IOException("could not parse URI " + config.getBaseUrl(), e); } } ... |
Somewhere you’d instantiate a HttpClient object. Then you get its […]