Upgrade class HttpsClientSslLiveTest

This commit is contained in:
DOHA 2014-11-26 10:25:33 +02:00
parent 5b416e68f7
commit 8a113ffec4
1 changed files with 68 additions and 59 deletions

View File

@ -7,6 +7,7 @@ import java.io.IOException;
import java.security.GeneralSecurityException; import java.security.GeneralSecurityException;
import java.security.cert.X509Certificate; import java.security.cert.X509Certificate;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLException; import javax.net.ssl.SSLException;
import org.apache.http.HttpResponse; import org.apache.http.HttpResponse;
@ -17,6 +18,7 @@ import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry; import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory; import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLContextBuilder; import org.apache.http.conn.ssl.SSLContextBuilder;
import org.apache.http.conn.ssl.SSLContexts;
import org.apache.http.conn.ssl.SSLSocketFactory; import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.conn.ssl.TrustSelfSignedStrategy; import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
import org.apache.http.conn.ssl.TrustStrategy; import org.apache.http.conn.ssl.TrustStrategy;
@ -79,12 +81,19 @@ public class HttpsClientSslLiveTest {
return true; return true;
} }
}; };
final SSLSocketFactory sf = new SSLSocketFactory(acceptingTrustStrategy, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); final SSLContext sslContext = SSLContexts.custom()
final SchemeRegistry registry = new SchemeRegistry(); .loadTrustMaterial(null, acceptingTrustStrategy).build();
registry.register(new Scheme("https", 443, sf));
final ClientConnectionManager ccm = new PoolingClientConnectionManager(registry); final SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
sslContext,
SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
final CloseableHttpClient httpClient = HttpClients
.custom()
.setHostnameVerifier(
SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER)
.setSSLSocketFactory(sslsf).build();
final CloseableHttpClient httpClient = new DefaultHttpClient(ccm);
final HttpGet getMethod = new HttpGet(HOST_WITH_SSL); final HttpGet getMethod = new HttpGet(HOST_WITH_SSL);
final HttpResponse response = httpClient.execute(getMethod); final HttpResponse response = httpClient.execute(getMethod);