java - https调用在直接从java调用时工作,但不能从oracle 12c内部工作
问题描述
错误是:sun.security.validator.ValidatorException:PKIX 路径验证失败:java.security.cert.CertPathValidatorException:时间戳检查失败
使用的代码是:
create or replace and compile java source named sendsms as
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
public class HttpsCall {
public static void main(String[] args) throws Exception {
String httpsURL = "https://www.jawalbsms.ws/api.php/sendsms?user=abc&pass=xyz&to=9665555555555&message=good morning&sender=abc";
URL myUrl = new URL(httpsURL);
HttpsURLConnection conn = (HttpsURLConnection) myUrl.openConnection();
InputStream is = conn.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String inputLine;
while ((inputLine = br.readLine()) != null) {
System.out.println(inputLine);
}
br.close();
} }
解决方案
通过在代码中添加以下行来解决,现在可以在 oracle 12c 上正常工作
SSLContext context = SSLContext.getInstance("TLSv1.2");
TrustManager[] trustManager = new TrustManager[] {
new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
public void checkClientTrusted(X509Certificate[] certificate, String str) {}
public void checkServerTrusted(X509Certificate[] certificate, String str) {}
}
};
context.init(null, trustManager, new SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());
javax.net.ssl.HttpsURLConnection.setDefaultHostnameVerifier(
new HostnameVerifier(){
public boolean verify(String hostname, SSLSession session) {
return true;
}
});
推荐阅读
- r - 如何在列表中插入占位符,其中使用 R 缺少数据片段?
- reactjs - Material-UI React:Paper 组件的全局主题覆盖
- javascript - 如何修复错误:“未定义项目”...
- html - 这个 HTML 列表的容器的 xpath 是什么?
- excel - 将数据行从表传输到不同工作表上的表
- python - 将单词列表(在数据框内)转换为一组单词
- swift4 - 当视图大小不同时如何以编程方式设置约束?
- c# - unity ui 和 collider2d
- python - 如何在 Python 中打印出时间对象的时区?
- python - 使用 base64 库中的 b32decode 进行 base32 解码时忽略填充异常