SNI support can be enabled when running on Oracle JRE 7 by using a custom SSL socket factory. Please note this code snippet makes use of Commons BeanUtils to invoke Oracle private method via reflection.

HttpClient 4.3

   1 SSLContext sslcontext = SSLContexts.createSystemDefault();
   2 SSLSocketFactory sslsf = new SSLSocketFactory(sslcontext) {
   3 
   4     @Override
   5     public Socket connectSocket(
   6         int connectTimeout, 
   7         Socket socket,
   8         HttpHost host, 
   9         InetSocketAddress remoteAddress,
  10         InetSocketAddress localAddress, 
  11         HttpContext context) throws IOException, ConnectTimeoutException {
  12         if (socket instanceof SSLSocket) {
  13             try {
  14                  PropertyUtils.setProperty(socket, "host", host.getHostName());
  15             } catch (NoSuchMethodException ex) {
  16             } catch (IllegalAccessException ex) {
  17             } catch (InvocationTargetException ex) {
  18             }
  19         }
  20         return super.connectSocket(connectTimeout, socket, host, remoteAddress,
  21             localAddress, context);
  22     }
  23 
  24 };
  25 
  26 CloseableHttpClient httpclient = HttpClients.custom()
  27     .setSSLSocketFactory(sslsf)
  28     .build();
  29 CloseableHttpResponse response = httpclient.execute(new HttpGet("https://verisign.com/"));
  30 try {
  31     System.out.println(response.getStatusLine());
  32     EntityUtils.consume(response.getEntity());
  33 } finally {
  34     response.close();
  35 }

SNISupport (last edited 2013-05-17 07:38:53 by OlegKalnichevski)