[英]Getting an exception javax.net.ssl.SSLHandshakeException: Received fatal alert: protocol_version
I have created a webcrawler, that will return the page for a URL.我创建了一个网络爬虫,它将返回一个 URL 的页面。 For some URLs I get : javax.net.ssl.SSLHandshakeException: Received fatal alert: protocol_version
对于我得到的一些 URL:javax.net.ssl.SSLHandshakeException: Received fatal alert: protocol_version
The purpose of this is just to parse the webpage returned.这样做的目的只是解析返回的网页。 So I am looking at ways to bypass.
所以我正在寻找绕过的方法。 The SSL verification
SSL验证
(s, sslSession) -> true
So I create a SSLConnectionSocketFactory所以我创建了一个 SSLConnectionSocketFactory
private SSLConnectionSocketFactory sslFactory() {
SSLContext sslcontext = null;
try {
sslcontext = SSLContexts.custom().loadTrustMaterial(null, new TrustSelfSignedStrategy()).build();
} catch (KeyManagementException | NoSuchAlgorithmException | KeyStoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslcontext, (s, sslSession) -> true);
return sslConnectionSocketFactory;
}
And then add it to the HTMLClient然后将其添加到 HTMLClient
var clientBuilder = HttpClientBuilder.create()
.setDefaultRequestConfig(RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD).build())
.setRedirectStrategy(new LaxRedirectStrategy())
.setDefaultCookieStore(cookieStore)
.setSSLSocketFactory(sslFactory());
According to what I have researched this should bypass SSL verification, but it still throws the exception.根据我的研究,这应该绕过 SSL 验证,但它仍然抛出异常。 I am using httpclient-4.5.13.
我正在使用 httpclient-4.5.13。
I understand the security implications, but this is parsing a webpage, it will not be sending any data.我了解安全隐患,但这是解析网页,它不会发送任何数据。 Thanks for any help.
谢谢你的帮助。
Ok this works with httpclient-4.5.13 java 11, it wont work with Java 16. So set trusted certificates.好的,这适用于 httpclient-4.5.13 java 11,它不适用于 Java 16。所以设置可信证书。
private SSLContext SSLConnection() throws NoSuchAlgorithmException, KeyManagementException {
TrustManager[] trustAllCerts = new TrustManager[] {
new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(X509Certificate[] certs, String authType) { }
public void checkServerTrusted(X509Certificate[] certs, String authType) { }
}
};
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
return sc;
}
And add it to clientBuilder并将其添加到 clientBuilder
var clientBuilder = HttpClientBuilder.create()
.setDefaultRequestConfig(RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD).build())
.setRedirectStrategy(new LaxRedirectStrategy())
.setDefaultCookieStore(cookieStore)
.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)
.setSSLContext(SSLConnection());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.