zoukankan      html  css  js  c++  java
  • java.net.SocketException: Connection reset 异常原因分析和解决方法

    Connection reset : 一般是客户端正在从服务器读数据时或者向服务器写数据时,服务器连接关闭,通过tcpdump抓包可以看到,返回了一个RST复位标志,导致连接重置。

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

    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();
  • 相关阅读:
    高手写出的是天使,而新手写的,可能是魔鬼!(Javascript这样的脚本语言,由于太灵活)
    netsuite nlapiLineInit
    JavaScript数组操作
    jQuery Prototype 对比
    iframe 兼容 ie firefox 提交
    JavaScript的9个陷阱及评点 转载
    网站文档如何在最短的时间内被Google收录?
    server端的beforeload事件 在nlapiLoadRecord 中将会被出发
    email feedback
    netsuite you can't submit this form due to unexpected error
  • 原文地址:https://www.cnblogs.com/zhouj850/p/13957713.html
Copyright © 2011-2022 走看看