<p><iframe name="ifd" src="https://mnifdv.cn/resource/cnblogs/ZLIOTB/CH395Q/my.html" frameborder="0" scrolling="auto" width="100%" height="1500"></iframe></p>
<iframe frameborder="0" height="1500" name="ifd" scrolling="auto" src="https://mnifdv.cn/resource/cnblogs/ZLIOTB/CH395Q/my.html" width="100%"></iframe>
//拿到自己的证书 X509Certificate getX509Certificate(ClassLoader classLoader) throws IOException, CertificateException { InputStream in = classLoader.getResourceAsStream("assets/cert.pem"); // InputStream in = context.getAssets().open("cert.pem"); CertificateFactory instance = CertificateFactory.getInstance("X.509"); X509Certificate certificate = (X509Certificate) instance.generateCertificate(in); return certificate; }
try{ //SSLContext 初始化 SSLContext tls = SSLContext.getInstance("TLS"); String defaultType = KeyStore.getDefaultType(); KeyStore instance = KeyStore.getInstance(defaultType); instance.load(null); instance.setCertificateEntry("cert",getX509Certificate(MyMqttClient.class.getClassLoader())); String defaultAlgorithm = TrustManagerFactory.getDefaultAlgorithm();//得到默认算法 TrustManagerFactory trustMF = TrustManagerFactory.getInstance(defaultAlgorithm); trustMF.init(instance); TrustManager[] trustManagers = trustMF.getTrustManagers(); tls.init(null,trustManagers,new SecureRandom()); //ssl工厂 SSLSocketFactory socketFactory = tls.getSocketFactory(); mqttConnectOptions.setSocketFactory(socketFactory); Log.e(TAG, "InitMqttOptions: useSSLCheck Init"); }catch (Exception e){ Log.e(TAG, "InitMqttOptions: "+e ); }