• 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);
    }

    解决

  • 相关阅读:
    js下载doxc 文件示例和部分后缀对应的content-type 总结
    使用react-app-rewired和customize-cra对默认webpack自定义配置
    koa2使用es7 的装饰器decorator
    vue history 模式打包部署在域名的二级目录的配置指南
    linux 安装 node 环境
    javascript 正则表达式之分组与前瞻匹配详解
    vue的$emit 与$on父子组件与兄弟组件的之间通信
    mysql 的基本操作总结--增删改查
    mysql 常用的时间日期函数小结
    小程序封装request请求,统一API
  • 原文地址:https://www.cnblogs.com/xint/p/11940245.html
Copyright © 2020-2023  润新知