首页 > 解决方案 > 如何使用 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。

避免诸如“您不应该信任”或“只需签名即可使用”之类的回答。

标签: androidreact-nativesslfetchself-signed

解决方案


我不得不在服务器端更改证书。由于 TSL2,我也遇到了类似的问题,因为 Android < 5 无法连接


推荐阅读