java - 微信红包https异常the trustAnchors parameter must be non-empty

查看:327
本文介绍了java - 微信红包https异常the trustAnchors parameter must be non-empty的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

在本地测试能通过,放到服务器上直接提示javax.net.ssl.SSLException: java.lang.RuntimeException: Unexpected error: java.security.InvalidAlgorithmParameterException: the trustAnchors parameter must be non-empty

所用https请求如下:
RedPackUtil.MCHId为商户id
RedPackUtil.CERT_FILE_PATH为证书的绝对地址
平台为windows,不知道有什么解决办法么?

    /**
     * 发送请求
     * */
    public static  String httpsRequestWX(String url,String data){
        StringBuffer message = new StringBuffer();
        try {
            String mchId = RedPackUtil.MCHId;
            KeyStore keyStore  = KeyStore.getInstance("PKCS12");

            String certFilePath = RedPackUtil.CERT_FILE_PATH;
            // linux下
            if ("/".equals(File.separator)) {
                certFilePath = "//usr//local//certs//apiclient_cert.p12";
            }
            FileInputStream instream = new FileInputStream(new File(certFilePath));
            keyStore.load(instream, mchId.toCharArray());
            SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, mchId.toCharArray()).build();
            SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[] { "TLSv1" }, null, SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
//            SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[] { "TLSv1" }, null, SSLConnectionSocketFactory.getDefaultHostnameVerifier());
            CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
            HttpPost httpost = new HttpPost(url);
            httpost.addHeader("Connection", "keep-alive");
            httpost.addHeader("Accept", "*/*");
            httpost.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
            httpost.addHeader("Host", "api.mch.weixin.qq.com");
            httpost.addHeader("X-Requested-With", "XMLHttpRequest");
            httpost.addHeader("Cache-Control", "max-age=0");
            httpost.addHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0) ");
            httpost.setEntity(new StringEntity(data, "UTF-8"));
            System.out.println("executing request" + httpost.getRequestLine());
            CloseableHttpResponse response = httpclient.execute(httpost);
            try {
                HttpEntity entity = response.getEntity();
                System.out.println("----------------------------------------");
                System.out.println(response.getStatusLine());
                if (entity != null) {
                    System.out.println("Response content length: " + entity.getContentLength());
                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(entity.getContent(),"UTF-8"));
                    String text;
                    while ((text = bufferedReader.readLine()) != null) {
                        message.append(text);
                    }
                }
                EntityUtils.consume(entity);
            } catch (IOException e) {
                logger.info("httpsRequestWX e"+e);
                e.printStackTrace();
            } finally {
                response.close();
            }
        } catch (Exception e1) {
            logger.info("httpsRequestWX e1"+e1);
            e1.printStackTrace();
        }
        logger.info("httpsRequestWX message"+message.toString());
        return message.toString();
    }

解决方案

结果发现是服务器的问题。。

这篇关于java - 微信红包https异常the trustAnchors parameter must be non-empty的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆