我正在尝试建立与HTTPS站点的连接,我得到了这个例外:javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
我原来的代码如下:
URL url = new URL("https://example.com");
HttpsURLConnection urlConnection =
(HttpsURLConnection)url.openConnection();
in = urlConnection.getInputStream();
byte[] responsedata = CommonUtil.readInputStream(in);
Log.w(TAG, "response is "+CommonUtil.convertBytesToHexString(responsedata));
Run Code Online (Sandbox Code Playgroud)
CertificateFactory cf;
try {
cf = CertificateFactory.getInstance("X.509");
InputStream in = this.mContext.getResources().openRawResource(R.raw.cert);
Certificate ca;
ca = cf.generateCertificate(in);
System.out.println("ca=" + ((X509Certificate) ca).getSubjectDN());
in.close();
URL url = new URL("https://example.com");
HttpsURLConnection urlConnection =
(HttpsURLConnection)url.openConnection();
in = urlConnection.getInputStream();
byte[] responsedata = CommonUtil.readInputStream(in);
Log.w(TAG, "response is "+CommonUtil.convertBytesToHexString(responsedata));
in.close();
Run Code Online (Sandbox Code Playgroud)
关于 …