我需要在 api 请求上发送 .CRT 和 .KEY 文件。我设法使用 Postman 完成了请求,但我不知道如何在 android 代码中传递密钥。
显像管:
-----BEGIN CERTIFICATE-----
-----END CERTIFICATE-----
Run Code Online (Sandbox Code Playgroud)
钥匙:
-----BEGIN RSA PRIVATE KEY-----
-----END RSA PRIVATE KEY-----
Run Code Online (Sandbox Code Playgroud)
这是我的客户
fun getClient(url: String, context: Context): Retrofit {
// Loading CAs from an InputStream
val cf: CertificateFactory = CertificateFactory.getInstance("X.509")
val ca: Certificate = context.resources.openRawResource(R.raw.crt).use { cert -> cf.generateCertificate(cert) }
// Creating a KeyStore containing our trusted CAs
val keyStoreType = KeyStore.getDefaultType()
val keyStore = KeyStore.getInstance(keyStoreType).apply {
load(null, null)
setCertificateEntry("ca", ca)
}
// Creating a TrustManager …
Run Code Online (Sandbox Code Playgroud)