我知道它之前已被問過,但我嘗試了所有我找到的解決方案,但仍然無法正常工作。Apache Http客戶端SSL證書錯誤
基本上,我試圖通過Apache Http Client(4.3)獲取一些內容,並且我連接的網站存在一些SSL問題。
首先,我得到了SSLException
和unrecognized_name
的消息。我試圖通過將jsse.enableSNIExtension
屬性設置爲false
來解決此問題。
然後,我得到這個異常: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
我又試圖提供我億韓元SSLFactory
,將接受所有證書,但我仍然得到同樣的異常。這裏是我的代碼:
private static void sslTest() throws Exception {
System.setProperty("jsse.enableSNIExtension", "false");
SSLContext sslContext = SSLContexts.custom()
.loadTrustMaterial(null, new TrustSelfSignedStrategy())
.useTLS()
.build();
SSLConnectionSocketFactory connectionFactory =
new SSLConnectionSocketFactory(sslContext, new AllowAllHostnameVerifier());
CookieStore cookieStore = new BasicCookieStore();
HttpClientContext context = HttpClientContext.create();
context.setCookieStore(cookieStore);
CloseableHttpClient httpclient = HttpClients.custom()
.setSSLSocketFactory(connectionFactory)
.setDefaultCookieStore(cookieStore)
.build();
URI uri = new URIBuilder()
.setScheme("https")
.setHost(BASE_URL)
.build();
String responseBody = httpclient.execute(new HttpGet(uri), RESPONSE_HANDLER);
}
所有幫助非常感謝!
請在另一個問題看我的答案:https://stackoverflow.com/a/45734000/8477758 – EyouGo