gpt4 book ai didi

java - jsp如何打开随机https url?

转载 作者:太空宇宙 更新时间:2023-11-03 13:21:39 25 4
gpt4 key购买 nike

我想读取用户输入的 https URL 的内容,我们没有任何关于证书 key 和 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 path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

如何强制jdk打开所有https url?

最佳答案

您可以尝试将此插入到您的 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*/

来源:How to ignore PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException?

关于java - jsp如何打开随机https url?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50104418/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com