在本地测试能通过,放到服务器上直接提示“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(); }
付费偷看金额在0.1-10元之间
一周热门 更多>