java.net.SocketException: Connection reset 异常原因分析和解决方法

导致此异常的原因,总结下来有三种情况:

1.服务器端偶尔出现了异常,导致连接关闭

解决方法: 采用出错重试机制

2.服务器端和客户端使用的连接方式不一致

解决方法:服务器端和客户端使用相同的连接方式,即同时使用长连接或短连接

3.如果是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”);

方法三:可以用以下工具类方法解决:

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;
    }
}
public class HttpsTrustManager implements X509TrustManager {

    @Override
    public void checkClientTrusted(X509Certificate[] arg0, String arg1)
            throws CertificateException {
        // TODO Auto-generated method stub

    }

    @Override
    public void checkServerTrusted(X509Certificate[] arg0, String arg1)
            throws CertificateException {
        // TODO Auto-generated method stub

    }

    @Override
    public X509Certificate[] getAcceptedIssuers() {
        return new X509Certificate[]{};
    }

}

调用方式如下:

  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();
  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值