java - jsp如何打开随机的https url?
问题描述
我想阅读用户输入的 https URL 的内容,我们没有关于证书密钥和 jks 等的任何数据:
<!DOCTYPE html>
<html>
<head>
<title>Total Synthesis of Fidaxomicin | | download</title>
</head>
<body>
<form action="testSSL.jsp">
<input name="sslRandomUrl" type="text">
<input type="submit" value="opensslUrl" >
</form>
</body>
这是jsp:
<%@page import="java.net.URL"%>
<%@page import="javax.net.ssl.HttpsURLConnection"%>
<%@page import="java.io.InputStreamReader"%>
<%@page import="java.io.BufferedReader"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%
String url = request.getParameter("sslRandomUrl");
System.out.println("How to open like web browser? >>>"+url);
URL pdfContainerUrl = new URL(url);
HttpsURLConnection conn2 = (HttpsURLConnection) pdfContainerUrl.openConnection();// دقت شود https
StringBuilder result = new StringBuilder();
conn2.setRequestMethod("GET");
BufferedReader br2 = new BufferedReader(new InputStreamReader(conn2.getInputStream(), "UTF-8"));
String line2;
while ((line2 = br2.readLine()) != null) {
result.append(line2);
System.out.println(line2);
}
out.print(result);
%>
但我对许多 URL 都有这个错误:
ValidatorException:PKIX 路径构建失败:sun.security.provider.certpath.SunCertPathBuilderException:无法找到请求目标的有效证书路径
如何强制 jdk 打开所有 https 网址?
解决方案
您可以尝试在您的 JSP 中插入它:
/* Start of Fix */
TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() { return null; }
public void checkClientTrusted(X509Certificate[] certs, String authType) { }
public void checkServerTrusted(X509Certificate[] certs, String authType) { }
} };
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
// Create all-trusting host name verifier
HostnameVerifier allHostsValid = new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) { return true; }
};
// Install the all-trusting host verifier
HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
/* End of the fix*/
来源:如何忽略 PKIX 路径构建失败:sun.security.provider.certpath.SunCertPathBuilderException?
推荐阅读
- c# - 模型冲突发生,因为它说 ViewDataDictionary 传递了模型 @model1 但需要 @model2 asp.net Identity 类型的模型
- javascript - 将专门用于列表的 futumorphism 表达为命令式循环
- python - 提高 python dblquad 和多处理的速度
- r - 如何在 dplyr 的 select_if 中使用 purrr 函数
- mysql - 在 mysql 中备份单个表
- node.js - sass-loader 无法解析 @import url
- python - 如何修复 Python 脚本的 launchctl 状态码 78?
- tensorflow - 在张量流中跨维度添加连续元素(np.add.reduceat)
- c++ - Delphi 库内存管理器奇怪
- python - 在页面上找不到数据