解决JDK7调用https报:java.net.SocketException: Connection reset错误

原因分析:

大多数现代的 HTTPS 连接将使用 TLS 1.2协议 或 TLS 1.3 协议,具体取决于服务器和客户端支持的版本以及其之间的协商,而JDK7及以下版本默认使用是TLS v1协议,所以在调用HTTPS接口时,会出现java.net.SocketException: Connection reset报错;

下面是不同JDK版本的默认TLS协议:

java 复制代码
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(默认)

解决方案:

手动设置TLS协议为v1.2版本,下面我提供了POST+JSON+用户认证请求(如果不需要用户认证的,可以去掉)代码示例供参考:

java 复制代码
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.codec.binary.Base64;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

public class HttpClientUtils {


    public static void main(String[] args) {
        try {
            String path = "https://XXXXXXXXXX";
            JSONObject param = new JSONObject();
            param.put("param1", "参数1");
            param.put("param2", "参数2");
            String resStr = HttpClientUtils.sendHttpsPostTLSAdd(path, param.toJSONString(), "用户账号", "用户密码", "UTF-8");
            System.out.println(resStr);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * post请求,设置TLS协议
     * @param url
     * @param json
     * @param username 用户名
     * @param password 密码
     * @param charSet 请求编码(UTF-8)
     * @return
     * @throws Exception
     */
    public static String sendHttpsPostTLSAdd(String url, String json, String username, String password, String charSet) throws Exception {
        // 实现一个X509TrustManager接口,用于绕过验证,不用修改里面的方法
        X509TrustManager trustManager = new X509TrustManager() {
            @Override
            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }

            @Override
            public void checkClientTrusted(X509Certificate[] arg0, String arg1)
                    throws CertificateException {

            }

            @Override
            public void checkServerTrusted(X509Certificate[] arg0, String arg1)
                    throws CertificateException {

            }

        };

        // 设置TLS协议版本为TLSv1.2
        SSLContext sc = SSLContext.getInstance("TLSv1.2");
        sc.init(null, new TrustManager[] { trustManager }, null);
        SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sc);

        // 得到httpClient对象
        CloseableHttpClient httpClient = null;
        HttpPost httpPost = null;
        String result = null;
        httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
        httpPost = new HttpPost(url);

        // 用户认证
        String authString = username + ":" + password;
        String encodedAuthString = Base64.encodeBase64String(authString.getBytes());
        httpPost.setHeader("Authorization", "Basic " + encodedAuthString);

        // 推送配置
        StringEntity entity = new StringEntity(json, charSet);
        entity.setContentEncoding(charSet); // 设置编码格式
        entity.setContentType("application/json"); // 设置JSON请求
        httpPost.setEntity(entity);
        // 执行
        HttpResponse response = httpClient.execute(httpPost);
        if (response != null) {
            HttpEntity resEntity = response.getEntity();
            if (resEntity != null) {
                result = EntityUtils.toString(resEntity, charSet);
            }
        }

        return result;
    }
}
相关推荐
学长毕设29 分钟前
基于SpringBoot 车辆租赁系统平台-附源码
java·spring boot·后端
Flynt2 小时前
Spring AI Alibaba 真停更了?我把它的 GitHub 翻了个底朝天
java·spring·开源
她的男孩6 小时前
Spring Boot 接 Flowable:请假单要同时走财务和行政,包容分支和默认分支怎么配
java·spring boot·工作流引擎
咖啡八杯7 小时前
LoginUser 适配器模式:适配 SysUser 到 UserDetails
java·开发语言·后端·框架·适配器模式·若依
北冥有鱼7 小时前
从 bean::method 里抠出注解:Java 方法引用元数据反查全记录
java·后端
CoderYanger8 小时前
Java EE 进阶:11. SpringBoot 统一功能处理(第四弹):@ControllerAdvice 源码分析+案例代码
java·数据库·spring boot·后端·spring·java-ee·mybatis
玖米GEO运营王总监11 小时前
抖音和视频号算法区别:玖米快跑的双端分发信号与内容适配对比
java·前端·人工智能
老秦包你会12 小时前
计算机网络第四课 ----Socket(套接字)编程TCP
网络协议·tcp/ip·计算机网络
AI人工智能+电脑小能手12 小时前
集合按大小切片分批,这个Java工具类5个方法含基本类型数组
java·工具类·批量处理·集合分片·分批调用
SimonKing13 小时前
高星红果短剧的TV版|桌面版都在这里了
java·后端·程序员