|
1 | 1 | package org.baeldung.httpclient; |
2 | 2 |
|
3 | | -import static org.hamcrest.Matchers.equalTo; |
4 | 3 | import static org.junit.Assert.assertThat; |
5 | 4 |
|
6 | 5 | import java.io.IOException; |
7 | 6 | import java.security.GeneralSecurityException; |
| 7 | +import java.security.KeyManagementException; |
| 8 | +import java.security.KeyStoreException; |
| 9 | +import java.security.NoSuchAlgorithmException; |
8 | 10 | import java.security.cert.X509Certificate; |
9 | 11 |
|
10 | 12 | import javax.net.ssl.SSLContext; |
|
16 | 18 | import org.apache.http.conn.ClientConnectionManager; |
17 | 19 | import org.apache.http.conn.scheme.Scheme; |
18 | 20 | import org.apache.http.conn.scheme.SchemeRegistry; |
| 21 | +import org.apache.http.conn.ssl.NoopHostnameVerifier; |
19 | 22 | import org.apache.http.conn.ssl.SSLConnectionSocketFactory; |
20 | 23 | import org.apache.http.conn.ssl.SSLContextBuilder; |
21 | 24 | import org.apache.http.conn.ssl.SSLContexts; |
@@ -108,4 +111,30 @@ public final void givenHttpClientPost4_3_whenAcceptingAllCertificates_thenCanCon |
108 | 111 | assertThat(response.getStatusLine().getStatusCode(), equalTo(200)); |
109 | 112 | } |
110 | 113 |
|
| 114 | + @Test |
| 115 | + public final void givenIgnoringCertificates_whenHttpsUrlIsConsumed_thenCorrect() throws ClientProtocolException, IOException { |
| 116 | + |
| 117 | + TrustStrategy acceptingTrustStrategy = new TrustStrategy() { |
| 118 | + @Override |
| 119 | + public boolean isTrusted(X509Certificate[] certificate, String authType) { |
| 120 | + return true; |
| 121 | + } |
| 122 | + }; |
| 123 | + |
| 124 | + SSLContext sslContext = null; |
| 125 | + try { |
| 126 | + sslContext = new SSLContextBuilder().loadTrustMaterial(null, acceptingTrustStrategy).build(); |
| 127 | + |
| 128 | + } catch (KeyManagementException | NoSuchAlgorithmException | KeyStoreException e) { |
| 129 | + e.printStackTrace(); |
| 130 | + } |
| 131 | + |
| 132 | + CloseableHttpClient client = HttpClients.custom().setSSLContext(sslContext).setSSLHostnameVerifier(new NoopHostnameVerifier()).build(); |
| 133 | + final HttpGet httpGet = new HttpGet("https://sesar3.geoinfogeochem.org/sample/igsn/ODP000002"); |
| 134 | + httpGet.setHeader("Accept", "application/xml"); |
| 135 | + |
| 136 | + final HttpResponse response = client.execute(httpGet); |
| 137 | + assertThat(response.getStatusLine().getStatusCode(), equalTo(200)); |
| 138 | + } |
| 139 | + |
111 | 140 | } |
0 commit comments