导致此异常的原因,总结下来有三种情况:
一、服务器端偶尔出现了异常,导致连接关闭
解决方法: 采用出错重试机制
二、 服务器端和客户端使用的连接方式不一致
解决方法: 服务器端和客户端使用相同的连接方式,即同时使用长连接或短连接
三、如果是HTTPS,那么还存在TLS版本不一致
解决方法: 服务器端和客户端使用相同的TLS版本,我遇到的就是这种情况。
附录:JDK中对 HTTPS 版本的支持情况:
JDK 6
SSL v3
TLS v1(默认)
TLS v1.1(JDK6 update 111 及以上)
JDK 7
SSLv3
TLS v1(默认)
TLS v1.1
TLS v1.2
JDK 8
SSL v3
TLS v1
TLS v1.1
TLS v1.2(默认)
方法一:如果客户端JDK是1.7,服务器端要求TLSv1.2,那么在启动参数加上-Dhttps.protocols=TLSv1.2即可。
方法二:代码指定TLS版本 System.setProperty(“https.protocols”, “TLSv1.2”); (我是通过这种方式,加了一行代码解决的)
方法三:可以用以下工具类方法解决:
javascript">public class HttpClientFactory {private static CloseableHttpClient client;public static HttpClient getHttpsClient() throws Exception {if (client != null) {return client;}SSLContext sslcontext = SSLContexts.custom().useSSL().build();sslcontext.init(null, new X509TrustManager[]{new HttpsTrustManager()}, new SecureRandom());SSLConnectionSocketFactory factory = new SSLConnectionSocketFactory(sslcontext,new String[] { "SSLv3", "TLSv1", "TLSv1.1", "TLSv1.2" }, null,SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);client = HttpClients.custom().setSSLSocketFactory(factory).build();return client;}public static void releaseInstance() {client = null;}
}
javascript">public class HttpsTrustManager implements X509TrustManager {@Overridepublic void checkClientTrusted(X509Certificate[] arg0, String arg1)throws CertificateException {// TODO Auto-generated method stub}@Overridepublic void checkServerTrusted(X509Certificate[] arg0, String arg1)throws CertificateException {// TODO Auto-generated method stub}@Overridepublic X509Certificate[] getAcceptedIssuers() {return new X509Certificate[]{};}}
调用方式如下:
javascript"> HttpClient httpClient = HttpClientFactory.getHttpsClient();HttpPost request = new HttpPost(requestUrl);request.setEntity(new StringEntity(gson.toJson(requestMap), "application/json", "UTF-8"));HttpResponse httpResponse = httpClient.execute(request);resultStr = EntityUtils.toString(httpResponse.getEntity(), "UTF-8");System.out.println(resultStr);httpResponse.getEntity().getContent().close();