【问题标题】:How to disable ssl certificate verification in react-native如何在 react-native 中禁用 ssl 证书验证
【发布时间】:2019-07-12 11:43:42
【问题描述】:

我正在用 react-native 构建一个应用程序。我正在尝试使用 fetch 方法在 react-native 中使用 API。 SSL 证书验证在关闭时会从 API 发出响应,否则会显示“无响应”。有没有办法在 react-native 中禁用 SSL 证书验证? 下面是我的代码 sn-p 用于访问 API。

fetch(('https:www.myUrl.com',{
       method: 'GET',
       headers:{
         'Accept': 'application/json',
         'Content-Type': 'application/json',
       },
}))
     .then((response) => response.json())
     .then((responseJson) => {
          console.log(JSON.stringify(responseJson))
     })
     .catch((err) => {
          Alert.alert("Request Failed")
          console.log(err.toString())
     });

【问题讨论】:

  • “有没有办法在 react-native 中禁用 SSL 证书验证?”这不是一个好主意,您几乎失去了 HTTPS 的所有好处。您不妨改为使用明文 HTTP...
  • @PatrickMevzek 我试过了,还是不行

标签: javascript node.js react-native ssl


【解决方案1】:

您将 fetch 的两个参数的括号合为一个,请考虑以下内容

fetch('https://slimparts.cap-tek.com:8080/v1/make', {
    method: 'GET',
    headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json',
    }
  })
  .then((response) => response.json())
  .then((responseJson) => {
    console.log(JSON.stringify(responseJson))
  })
  .catch((err) => {
    console.log(err.toString())
  });
  
  

【讨论】:

  • 通常这段代码可以工作,但是当 SSL 证书验证被禁用时,它会报错
  • 添加crossDomain:true, 作为属性可能吗?
  • 你在 iOS 上测试吗?
  • 我在安卓上测试
  • @shrys crossDomain 不是 fetch 的属性
猜你喜欢
  • 1970-01-01
  • 2019-06-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-27
  • 2014-02-14
  • 2011-05-03
  • 1970-01-01
相关资源
最近更新 更多