用例:有一个 Web 服务发布为https://abcd/zz?wsdl
我想做的是查询这个 URI,如果我得到一个有效的 WSDL,我返回一个布尔值“true”,否则返回一个“false”。现在,如果我通过 Chrome 浏览器访问这个 URL,我将不得不手动接受在证书警告上,然后下载 WSDL。但是如何通过 Java / HttpsURLConnection 做到这一点
import java.net.URL;
import java.io.*;
import javax.net.ssl.HttpsURLConnection;
public class JavaHttpsExample
{
public static void main(String[] args)
throws Exception
{
String httpsURL = "https://a.b.c.d/zz/V2.0/api?wsdl";
URL myurl = new URL(httpsURL);
HttpsURLConnection con = (HttpsURLConnection)myurl.openConnection();
InputStream ins = con.getInputStream();
InputStreamReader isr = new InputStreamReader(ins);
BufferedReader in = new BufferedReader(isr);
String inputLine;
while ((inputLine = in.readLine()) != null)
{
System.out.println(inputLine);
}
in.close();
}
}
我得到一个错误:
线程“主”javax.net.ssl.SSLHandshakeException 中的异常:java.security.cert.CertificateException:在 com.sun.net.ssl.internal.ssl.Alerts.getSSLException 中找不到与 IP 地址 abcd 匹配的主题替代名称(未知来源) 在 com.sun.net.ssl.internal.ssl.SSLSocketImpl.fatal(Unknown Source) 在 com.sun.net.ssl.internal.ssl.Handshaker.fatalSE(Unknown Source) 在 com.sun.net.ssl。 internal.ssl.Handshaker.fatalSE(Unknown Source) at com.sun.net.ssl.internal.ssl.ClientHandshaker.serverCertificate(Unknown Source) at com.sun.net.ssl.internal.ssl.ClientHandshaker.processMessage(Unknown Source ) 在 com.sun.net.ssl.internal.ssl.Handshaker.process_record(Unknown Source) 在 com.sun.net.ssl 的 com.sun.net.ssl.internal.ssl.Handshaker.processLoop(Unknown Source)。 internal.ssl.SSLSocketImpl。readRecord(Unknown Source) at com.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source) at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(Unknown Source) at com.sun。 net.ssl.internal.ssl.SSLSocketImpl.startHandshake(Unknown Source) at sun.net.www.protocol.https.HttpsClient.afterConnect(Unknown Source) at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(Unknown Source ) 在 sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source) at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source) at JavaHttpsExample.main(JavaHttpsExample.java:14) :java.security.cert.CertificateException:在 sun.security.util.HostnameChecker 中找不到与 IP 地址 abcd 匹配的主题替代名称。matchIP(Unknown Source) at sun.security.util.HostnameChecker.match(Unknown Source) at com.sun.net.ssl.internal.ssl.X509TrustManagerImpl.checkIdentity(Unknown Source) at com.sun.net.ssl.internal。 ssl.X509TrustManagerImpl.checkServerTrusted(未知来源)
我已经用 abcd 替换了真实 IP(当然)