如何在带有React Native的Android上允许自签名证书

时间:2018-09-25 11:23:40

标签: android react-native ssl fetch self-signed

我正在开发一个React Native App发出http / https请求。我正在为Android和iOS平台做这件事。我面临的一个问题是不允许我使用自签名证书获取网址。我已经测试过,这可以与其他未自签名的网址一起使用。有一种方法可以强制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上失败。如果该网址不是自签名证书,则此代码可在Android上使用。

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

1 个答案:

答案 0 :(得分:0)

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