• Remote host closed connection during handshake


    /**
    * dopost请求
    * @param url
    * @param msgEn
    * @return
    */
    private String doPost(String url, String msgEn) throws Exception {
    URL urlServlet = null;
    BufferedReader reader = null;
    StringBuffer lines = new StringBuffer();
    try {
    urlServlet = new URL(url);
    HttpURLConnection urlConnect = (HttpURLConnection) urlServlet
    .openConnection();
    // 设置连接参数
    urlConnect.setRequestMethod("POST");
    urlConnect.setDoInput(true);
    urlConnect.setDoOutput(true);
    urlConnect.setRequestProperty("Content-type", "application/json");
    urlConnect.setAllowUserInteraction(true);

    // 开启流,写入流数据
    if (null != msgEn || "".equals(msgEn)) {
    OutputStream output = urlConnect.getOutputStream();
    output.write(msgEn.getBytes("UTF-8"));
    output.flush();
    output.close();
    }
    // 获取返回输出流
    InputStream input = urlConnect.getInputStream();
    reader = new BufferedReader(new InputStreamReader(input,"GBK"));
    String strMesg = "";
    while ((strMesg = reader.readLine()) != null) {
    lines.append(strMesg);
    }
    } catch (IOException e) {
    throw new Exception("电子合同签约失败:" + e.getMessage());
    } finally {
    if (reader != null) {
    try {
    reader.close();
    } catch (IOException e) {
    throw new Exception("电子合同开户关闭流失败:" + e.getMessage());
    }
    }
    }
    return lines.toString();
    }

    用上述方法访问http请求时,没问题,如果访问https请求时报错Remote host closed connection during handshake,网上搜了搜大体意思是

    1. jdk1.7默认是TSLv1, 但是可以支持TSLv1.1,TSLv1.2,jdk1.8默认是TSLv1.2

    2.如果客服端是TSLv1,服务器端设置是TSLv1.2,访问会出现connection reset的错误.

    我搜了一下工程,用该方法可以解决:

    private String doHttpsPost(String url, String strData) throws Exception {
    URL uploadServlet = null;
    BufferedReader reader = null;
    StringBuffer lines = new StringBuffer();
    try {
    TrustManager[] tm = { new X509TrustManager() {
    @Override
    public void checkClientTrusted(X509Certificate[] chain,
    String authType) throws CertificateException {
    }
    @Override
    public void checkServerTrusted(X509Certificate[] chain,
    String authType) throws CertificateException {
    }
    @Override
    public X509Certificate[] getAcceptedIssuers() {
    return null;
    }
    } };
    // 创建SSLContext
    SSLContext sslContext = SSLContext.getInstance("SSL");
    // 初始化
    sslContext.init(null, tm, new java.security.SecureRandom());
    // 获取SSLSocketFactory对象
    SSLSocketFactory ssf = sslContext.getSocketFactory();
    System.setProperty("https.protocols", "TLSv1.2,TLSv1.1,SSLv3");

    uploadServlet = new URL(url);
    HttpsURLConnection servletConnection = (HttpsURLConnection) uploadServlet.openConnection();//在此获取https连接
    // 设置连接参数
    servletConnection.setRequestMethod("POST");
    servletConnection.setDoOutput(true);
    servletConnection.setDoInput(true);
    servletConnection.setUseCaches(false);
    servletConnection.setRequestProperty("Content-type", "application/json");
    servletConnection.setAllowUserInteraction(true);
    servletConnection.setConnectTimeout(10000);
    servletConnection.setReadTimeout(10000);
    // 设置当前实例使用的SSLSoctetFactory
    servletConnection.setSSLSocketFactory(ssf);
    servletConnection.connect();


    // 开启流,写入数据
    if(strData!=null && !"".equals(strData)){
    OutputStream output = servletConnection.getOutputStream();
    output.write(strData.getBytes("UTF-8"));
    output.flush();
    output.close();
    }

    // 获取返回的数据
    InputStream inputStream = servletConnection.getInputStream();
    reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
    String line = "";
    while ((line = reader.readLine()) != null) {
    lines.append(line);
    }
    } catch (Exception e) {
    throw new Exception("电子合同签约失败:" + e.getMessage());
    } finally {
    if (reader != null) {
    try {
    reader.close();
    } catch (IOException e) {
    throw new Exception("电子合同开户关闭流失败:" + e.getMessage());
    }
    }
    }
    return lines.toString();
    }

    但是上边方法对http会有问题,所以可以用该逻辑

    if(url.toLowerCase().startsWith("https")){

    userid = doHttpsPost(url,requestStr);
    } else {
    userid = doPost(url,requestStr);
    }

    解决

  • 相关阅读:
    域账户-配置文件
    创建任务计划
    查看系统和PowerShell版本
    查找数组中元素的索引位置
    更改计算机名称,修改密码
    生成GUID
    微服务架构 vs. SOA架构
    springMvc架构简介
    Spring Boot和Spring cloud
    Spring链接汇总
  • 原文地址:https://www.cnblogs.com/xint/p/11940245.html
Copyright © 2020-2023  润新知