Remote host closed connection during handshake问题解决方法

最近在项目开发中用OkHttpClient获取网络上的图片,本地开发一切正常,结果放到服务器上,获取图片频繁失败,查看日志发现报错:

javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake
	at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:992)
	at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1375)
	at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1403)
	at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1387)
	at okhttp3.internal.connection.RealConnection.connectTls(RealConnection.java:336)
	at okhttp3.internal.connection.RealConnection.establishProtocol(RealConnection.java:300)
	at okhttp3.internal.connection.RealConnection.connect(RealConnection.java:185)
	at okhttp3.internal.connection.ExchangeFinder.findConnection(ExchangeFinder.java:224)
	at okhttp3.internal.connection.ExchangeFinder.findHealthyConnection(ExchangeFinder.java:108)
	at okhttp3.internal.connection.ExchangeFinder.find(ExchangeFinder.java:88)
	at okhttp3.internal.connection.Transmitter.newExchange(Transmitter.java:169)
	at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:41)
	at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142)
	at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:117)
	at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:94)
	at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142)
	at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:117)
	at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
	at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142)
	at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:88)
	at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142)
	at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:117)
	at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:221)
	at okhttp3.RealCall.execute(RealCall.java:81)
	at com.jftech.investment.cloud.impl.sync.NewInformationSyncService.getImgFileObject(NewInformationSyncService.java:469)
	at com.jftech.investment.cloud.impl.sync.NewInformationSyncService.getThumbnailId(NewInformationSyncService.java:505)
	at com.jftech.investment.cloud.impl.sync.NewInformationSyncService.getThumbnailId(NewInformationSyncService.java:519)
	at com.jftech.investment.cloud.impl.sync.NewInformationSyncService.getMorningPost(NewInformationSyncService.java:537)
	at com.jftech.investment.cloud.impl.sync.NewInformationSyncService.saveMorningPost(NewInformationSyncService.java:334)
	at com.jftech.investment.cloud.impl.sync.NewInformationSyncService.lambda$null$1(NewInformationSyncService.java:226)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
	at java.lang.Thread.run(Thread.java:748)
Caused by: java.io.EOFException: SSL peer shut down incorrectly
	at sun.security.ssl.InputRecord.read(InputRecord.java:505)
	at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:973)
	... 32 common frames omitted

网上查了下原因:

jre的加密策略原因,导致SSL请求还没发出去,客户端就自己断了连接。网上说的是TLS版本的问题,jdk1.7默认的TLS版本是1.0,将TLS版本改成1.1或者1.2就好了。

可是我明明用的是jdk1.8呀,继续找解决方案,在stackoverflow上找到了解决的方法:https://stackoverflow.com/questions/21245796/javax-net-ssl-sslhandshakeexception-remote-host-closed-connection-during-handsh

解决方法如下:

在代码中发送http请求之前,加上 如下代码:

System.setProperty("https.protocols", "TLSv1,TLSv1.1,TLSv1.2");

测试后,发现图片可以正常下载了,问题解决。

注意:System.setProperty是全局生效,所以我把这行代码放在了静态代码块中,这样的话这行代码只会被执行一次。

 

你可能感兴趣的:(java)