https请求微信分账接口 使用证书双向认证,
在本地可以收到返回 在Linux服务器中发起请求之后报错
java.lang.IllegalArgumentException: no SSLSocketFactory specified
在本地跑是没问题的 一上服务器就挂了 ,证书认证也是加上了的,请求帮助 谢谢!
public static String sendXmlWithCertificate(String urlStr, String xmlInfo, String mchId) {
BufferedReader br = null;
try {
URL url = new URL(urlStr);
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setRequestMethod("POST");
// 设置通用的请求属性
connection.setUseCaches(false);
connection.setDoOutput(true);
connection.setDoInput(true);
//设置报文格式
connection.setRequestProperty("Content-Type", "text/xml");
//证书双向验证
connection.setSSLSocketFactory(readCertificate(mchId));
// 得到请求的输出流对象
DataOutputStream out = new DataOutputStream(connection.getOutputStream());
out.write(xmlInfo.getBytes(StandardCharsets.UTF_8));
out.flush();
out.close();
// 建立实际的连接
connection.connect();
// 定义 BufferedReader输入流来读取URL的响应
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8));
StringBuilder result = new StringBuilder();
String getLine;
while ((getLine = in.readLine()) != null) {
result.append(getLine);
}
in.close();
return result.toString();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static SSLSocketFactory readCertificate(String mchId) {
try {
KeyStore keyStore = KeyStore.getInstance("PKCS12");
String filePath = Class.class.getResource("/").getPath() + "wxpaycert/apiclient_cert_" + mchId + ".p12";
FileInputStream stream = new FileInputStream(filePath);
keyStore.load(stream, mchId.toCharArray());
stream.close();
SSLContext sslContext = SSLContexts.custom().loadKeyMaterial(keyStore, mchId.toCharArray()).build();
return sslContext.getSocketFactory();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}