android - 如何使用 React Native 在 Android 上允许自签名证书
问题描述
我正在开发一个 React Native App 发出 http / https 请求。我正在为Android和iOS两个平台做。我面临一个问题,不允许我使用自签名证书获取 url。我已经测试过这适用于其他非自签名的 url。有没有办法强制 React Native 信任自签名证书?
执行该fetch()
函数时出现的错误是:
TypeError: Network request failed
在这里我发布我的代码:
const form = new FormData();
form.append('parameters[ticketPerson]', this.state.ticketPerson);
form.append('rawxmlresponse', 'false');
fetch(this.state.url, {
method: 'POST', headers: { Connection: 'close' },
body: form,
})
.then(response => response.json())
.then(async (responseJson) => {
console.log('repsonse --->', responseJson);
})
.catch((error) => {
console.log('ERROR ---> ', error);
});
它适用于 iOS,仅在 Android 上失败。如果 url 不是自签名证书,则此代码适用于 Android。
避免诸如“您不应该信任”或“只需签名即可使用”之类的回答。
解决方案
我不得不在服务器端更改证书。由于 TSL2,我也遇到了类似的问题,因为 Android < 5 无法连接
推荐阅读
- reactjs - 在反应事件冒泡中使用 e.preventDefault 取消轮子事件
- python - 如何使用python按日期分组数据并同时获取模式
- java - JPA 规范 - 使用拆分为两个数据库列的嵌套字段创建查询
- rstudio - 无法启动拨浪鼓 - “内存未映射”
- xml - 如何在 cq 对话框中动态使用下拉元素更改选项?
- laravel - 如何在 cPanel 中与 laravel 网页一起运行 websocket?
- ros - 如何使 ardrone 自治包在 Jetson Nano 上工作?
- java - 我应该更改/更改 xjc 生成的 java 文件吗?
- opencv - 如何从 Python OpenCV 从模拟摄像机(HIWATCH DS-T101)获取视频
- python - 如何在熊猫中使用 groupby 查找百分比 - 不是总数,而是单个邻域过滤器