HttpClient高级进阶-SSL

简介

本文将展示如何使用“全部接受”SSL支持配置Apache HttpClient 4。目标很简单 - 使用没有有效证书的HTTPS URL。

SSLPeerUnverifiedException

如果不使用HttpClient配置SSL ,以下测试(使用HTTPS URL)将失败:

public class RestClientLiveManualTest {
 
    @Test(expected = SSLPeerUnverifiedException.class)
    public void test() 
      throws ClientProtocolException, IOException {
  
        CloseableHttpClient httpClient = HttpClients.createDefault();
        String urlOverHttps
        ="https://localhost:8082/httpclient-simple)";

HttpGet getMethod = new HttpGet(urlOverHttps);
         
        HttpResponse response = httpClient.execute(getMethod);
        assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
    }
}

异常报错为:

javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated
    at sun.security.ssl.SSLSessionImpl.getPeerCertificates(SSLSessionImpl.java:397)
 

你可能感兴趣的:(java,职场与发展,ssl,java,https)