Source From Here
Preface
How to ignore SSL certificate (trust all) for Apache HttpClient 4.3? All the answers that I have found are previous versions, and the API changed. Related:
How-To
The code below works for trusting self-signed certificates. You have to use the TrustSelfSignedStrategy when creating your client:
I did not include the
SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER on purpose: The point was to allow testing with self signed certificates and not having to acquire a proper certificate from a certification authority. You can easily create a self-signed certificate with the correct host name, so do that instead of adding the SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER flag.
Below is an example of accessing "HttpComponents Downloads" page:
Preface
How to ignore SSL certificate (trust all) for Apache HttpClient 4.3? All the answers that I have found are previous versions, and the API changed. Related:
How-To
The code below works for trusting self-signed certificates. You have to use the TrustSelfSignedStrategy when creating your client:
- SSLContextBuilder builder = new SSLContextBuilder();
- builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
- SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
- builder.build());
- CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(
- sslsf).build();
- HttpGet httpGet = new HttpGet("https://some-server");
- CloseableHttpResponse response = httpclient.execute(httpGet);
- try {
- System.out.println(response.getStatusLine());
- HttpEntity entity = response.getEntity();
- EntityUtils.consume(entity);
- }
- finally {
- response.close();
- }
Below is an example of accessing "HttpComponents Downloads" page:
- package test;
- import org.apache.http.HttpEntity;
- import org.apache.http.client.methods.CloseableHttpResponse;
- import org.apache.http.client.methods.HttpGet;
- import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
- import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
- import org.apache.http.impl.client.CloseableHttpClient;
- import org.apache.http.impl.client.HttpClients;
- import org.apache.http.ssl.SSLContextBuilder;
- import org.apache.http.util.EntityUtils;
- public class HttpsGetEx {
- public static void main(String[] args) throws Exception
- {
- SSLContextBuilder builder = new SSLContextBuilder();
- builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
- SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
- builder.build());
- CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(
- sslsf).build();
- HttpGet httpGet = new HttpGet("https://hc.apache.org/downloads.cgi");
- CloseableHttpResponse response = httpclient.execute(httpGet);
- try {
- System.out.println(response.getStatusLine());
- HttpEntity entity = response.getEntity();
- System.out.printf("\t[Info] Page Content:\n%s\n", EntityUtils.toString(entity));
- }
- finally {
- response.close();
- }
- }
- }
沒有留言:
張貼留言