1.Android 添加Https证书
public static SSLSocketFactory getSSlSocketFactory(InputStream certificates) {
SSLContext sslContext = null;
try {
CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
Certificate ca;
try {
ca = certificateFactory.generateCertificate(certificates);
} finally {
certificates.close();
}
// Create a KeyStore containing our trusted CAs
String keyStoreType = KeyStore.getDefaultType();
KeyStore keyStore = KeyStore.getInstance(keyStoreType);
keyStore.load(null, null);
keyStore.setCertificateEntry("ca", ca);
// Create a TrustManager that trusts the CAs in our KeyStore
String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
tmf.init(keyStore);
// Create an SSLContext that uses our TrustManager
sslContext = SSLContext.getInstance("SSL");
sslContext.init(null, tmf.getTrustManagers(), null);
} catch (Exception e) {
LogUtil.e(TAG, "getSSlSocketFactory exception: " + e.getMessage());
}
return sslContext != null ? sslContext.getSocketFactory() : null;
}
public static InputStream getInputStream(Context context, String fileName) {
InputStream inputStream = null;
try {
inputStream = context.getAssets().open(fileName);
} catch (IOException e) {
LogUtil.i(TAG, "getInputStream file name: " + fileName + " exception: " + e.getMessage());
}
return inputStream;
}