webmagic爬取https网站,报错javax.net.ssl.SSLHandshakeException: Received fatal alert: protocol_version

针对WebMagic在爬取仅支持TLS1.2的HTTPS站点时出现的SSLHandshakeException错误,本文提供了修改HttpClientDownloader以支持多种SSL/TLS版本的解决方案,通过自定义SSLConnectionSocketFactory实现,确保爬虫正常运行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目前webmagic最新版是0.7.3版本,在爬取只支持TLS1.2https站点会报错,作者说会在0.7.4版本发布修复错误,但是等了3年了都没等到0.7.4版本的发布.

javax.net.ssl.SSLHandshakeException: Received fatal alert: protocol_version
	at sun.security.ssl.Alert.createSSLException(Alert.java:131)
	at sun.security.ssl.Alert.createSSLException(Alert.java:117)
	at sun.security.ssl.TransportContext.fatal(TransportContext.java:314)
	at sun.security.ssl.Alert$AlertConsumer.consume(Alert.java:293)
	at sun.security.ssl.TransportContext.dispatch(TransportContext.java:187)
	at sun.security.ssl.SSLTransport.decode(SSLTransport.java:154)
	at sun.security.ssl.SSLSocketImpl.decode(SSLSocketImpl.java:1198)
	at sun.security.ssl.SSLSocketImpl.readHandshakeRecord(SSLSocketImpl.java:1107)
	at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:400)
	at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:372)
	at org.apache.http.conn.ssl.SSLConnectionSocketFactory.createLayeredSocket(SSLConnectionSocketFactory.java:436)
	at org.apache.http.conn.ssl.SSLConnectionSocketFactory.connectSocket(SSLConnectionSocketFactory.java:384)
	at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:142)
	at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:376)
	at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:393)
	at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:236)
	at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:186)
	at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:89)
	at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
	at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185)
	at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83)
	at us.codecraft.webmagic.downloader.HttpClientDownloader.download(HttpClientDownloader.java:85)
	at us.codecraft.webmagic.Spider.processRequest(Spider.java:404)
	at us.codecraft.webmagic.Spider.access$000(Spider.java:61)
	at us.codecraft.webmagic.Spider$1.run(Spider.java:320)
	at us.codecraft.webmagic.thread.CountableThreadPool$1.run(CountableThreadPool.java:74)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at java.lang.Thread.run(Thread.java:748)

目前的解决办法是:临时适配方式,修改HttpClientGenerator中的buildSSLConnectionSocketFactory方法,重写自己实现的HttpClientDownloader,并设置到Spider中。修改内容如下:

return new SSLConnectionSocketFactory(createIgnoreVerifySSL(), new String[]{"SSLv3", "TLSv1", "TLSv1.1", "TLSv1.2"},
                    null,
                    new DefaultHostnameVerifier())

目前我已经把源码修改好了,并编译为jar包,有被同样问题困扰的朋友可以直接下载使用.

webmagic-core-0.7.3.jar

提取码:webm

jar包使用方法:将下载好的 jar包,放在你的maven仓库的us\codecraft\webmagic-core\0.7.3路径下,替换原有jar包即可解决问题.

 

javax.net.ssl.SSLHandshakeException: Received fatal alert: protocol_version 是一个常见的SSL握手异常,表示在SSL/TLS协议握手过程中,客户端和服务器之间无法就使用的协议版本达成一致。通常情况下,这个异常是由于以下原因引起的: 1. **协议版本不兼容**:客户端和服务器支持的TLS/SSL协议版本不一致。例如,客户端可能只支持TLS 1.2,而服务器只支持TLS 1.0。 2. **过时的协议**:服务器或客户端使用了一个过时的、被认为不安全的协议版本,如SSL 3.0或TLS 1.0。 3. **配置问题**:服务器或客户端的SSL配置不正确,导致无法协商出合适的协议版本。 解决这个问题的常见方法包括: 1. **更新协议版本**:确保客户端和服务器都支持最新的TLS版本(如TLS 1.2或TLS 1.3)。 2. **检查配置**:检查服务器和客户端的SSL配置,确保它们启用了兼容的协议版本。 3. **升级软件**:如果使用的是过时的库或软件,考虑升级到支持最新TLS版本的新版本。 以下是一个示例代码,展示如何在Java中强制使用TLS 1.2: ```java import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.SSLContext; import java.security.NoSuchAlgorithmException; public class SSLExample { public static void main(String[] args) { try { SSLContext sslContext = SSLContext.getInstance("TLSv1.2"); sslContext.init(null, null, null); HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory()); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } } } ``` 在这个示例中,我们强制使用TLS 1.2作为默认的SSL协议版本。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值