微信红包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();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
结果发现是服务器的问题。。