android - 如何使用 Retrofit 的输入流加载 SSL 证书

标签 android ssl kotlin retrofit2

我需要为 Android 中的 Retrofit 添加我的 SSL 证书,但我无法使用 Google 的示例代码找到该文件。

这是我使用的 Google 示例 https://developer.android.com/training/articles/security-ssl

运行时:

val caInput: InputStream = BufferedInputStream(FileInputStream("rest_of_the_world_production.crt"))

我得到了错误

 Caused by: java.io.FileNotFoundException: rest_of_the_world_production.crt: open failed: ENOENT (No such file or directory)

并在尝试访问文件时立即崩溃。该文件当前作为 crt 文件存储在 res/raw/rest_of_the_world_production.crt 下,为什么 Android 找不到它?

最佳答案

按如下方式进行:

InputStream cert = context.getResources().openRawResource(R.raw.my_cert); 
// Place your 'my_cert.crt' file in `res/raw`

关于android - 如何使用 Retrofit 的输入流加载 SSL 证书,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58798013/

相关文章:

node.js - "This Connection is Untrusted"但仅限于 firefox

javascript - node.js 中 STARTTLS 的特定 TLS 版本

apache - 使用 OpenSUSE/Yast 设置 Apache w/SSL

swift - Kotlin 有像 Swift 这样的 Result 类型吗

android - 将包含二进制文件的文件夹结构复制到 native 库文件夹

java - 在 Android 中,有没有使用类来格式化一组 View 的方法?

android - Kotlin:单元测试将函数作为参数的函数

java - 无法在 android 中加载库 "opencv_java4"

javascript - Android WebView 不显示 javascript 动画

php - android + php + mysql +注册错误