TrustManager[] trustmanagers = tmfactory.getTrustManagers();
SSLContext sslcontext = SSLContext.getInstance("TLSv1");
sslcontext.init(keymanagers, trustmanagers, null);
LocalTestServer server = new LocalTestServer(null, null, null, sslcontext);
server.registerDefaultHandlers();
server.start();
try {
TestX509HostnameVerifier hostnameVerifier = new TestX509HostnameVerifier();
SSLSocketFactory socketFactory = new SSLSocketFactory(sslcontext);
socketFactory.setHostnameVerifier(hostnameVerifier);
Scheme https = new Scheme("https", socketFactory, 443);
DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient.getConnectionManager().getSchemeRegistry().register(https);
HttpHost target = new HttpHost(
LocalTestServer.TEST_SERVER_ADDR.getHostName(),
server.getServicePort(),
"https");
HttpGet httpget = new HttpGet("/random/100");
HttpResponse response = httpclient.execute(target, httpget);
assertEquals(200, response.getStatusLine().getStatusCode());
assertTrue(hostnameVerifier.isFired());
} finally {
server.stop();
}
}