我正在使用com.loopj.android:android-async-http:1.4.9我的服务器请求。在我的服务器中要求我使用SSL / TLS之前,它一直运行良好。因此,我需要修改AsyncHTTPClient以在所有URL中使用HTTPS。
我检查了类似的方法,如何使用AsyncHttpClient进行HTTPS调用?但没有提供明确的解决方案。由于库本身发出以下警告,因此可接受的解决方案也不安全:
警告!这会忽略每台设备上的SSL证书验证,请谨慎使用。
所以我继续检查其他解决方案。我最终遵循了以下建议:https : //developer.android.com/training/articles/security-ssl.html。因此,我有类似的东西:
// Load CAs from an InputStream
// (could be from a resource or ByteArrayInputStream or ...)
CertificateFactory cf = CertificateFactory.getInstance("X.509");
// From https://www.washington.edu/itconnect/security/ca/load-der.crt
InputStream caInput = new BufferedInputStream(new FileInputStream("load-der.crt"));
Certificate ca;
try {
ca = cf.generateCertificate(caInput);
System.out.println("ca=" + ((X509Certificate) ca).getSubjectDN());
} finally {
caInput.close();
}
// Create a KeyStore containing our trusted CAs
String keyStoreType = KeyStore.getDefaultType();
KeyStore keyStore = KeyStore.getInstance(keyStoreType);
keyStore.load(null, null);
keyStore.setCertificateEntry("ca", …Run Code Online (Sandbox Code Playgroud)