如果ssl(https)证书无效,则React Native XMLHttpRequest请求将失败 [英] React Native XMLHttpRequest request fails if ssl (https) certificate is not valid

查看:460
本文介绍了如果ssl(https)证书无效,则React Native XMLHttpRequest请求将失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用无效证书对XML服务器执行XMLHttpRequest时,React Native会抛出异常操作无法完成。(NSURLErrorDomain错误-1202。)

When i am doing XMLHttpRequest to https server with not valid certificate React Native throws exception "The operation couldn’t be completed. (NSURLErrorDomain error -1202.)"

是否可以禁用对React Native XMLHttpRequest的ssl警告/检查?

Is it possible to disable ssl warning / checks for React Native XMLHttpRequest?

推荐答案

免责声明:此解决方案应该是临时的并记录在案它不会停留在软件的生产阶段,这只适用于开发。

Disclaimer: This solution should be temporary and documented so that it won't stay in the production phase of the software, this is for development only.

对于iOS,您所要做的就是打开您的xcodeproject(里面) RN中的iOS文件夹一旦打开,转到RCTNetwork.xcodeproj并在该项目中导航到RCTHTTPRequestHandler.m

For iOS, all you have to do is, open your xcodeproject (inside your iOS folder in RN) once you have that open, go to RCTNetwork.xcodeproj and in that project, navigate to RCTHTTPRequestHandler.m

在该文件中,您将看到一行像这样:

In that file you will see a line like this:

 #pragma mark - NSURLSession delegate

在该行之后,添加此函数

right after that line, add this function

- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler
{
  completionHandler(NSURLSessionAuthChallengeUseCredential, [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]);
}

瞧,您现在可以在没有有效证书的情况下对您的API进行不安全的调用。

And voila, you can now make insecure calls to your API without a valid certificate.

这应该足够了,但是如果你仍然遇到问题,你可能需要转到项目的info.plist,左键单击它并选择打开为。 ..源代码。

That should be enough, but if you are still having problems, you might need to go to your project's info.plist, left click on it and choose open as... source code.

最后只需添加

<key>NSAppTransportSecurity</key>
  <dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
  </dict>

所以你的文件看起来像这样

so your file will look like this

    ...
    <key>UISupportedInterfaceOrientations</key>
    <array>
        <string>UIInterfaceOrientationPortrait</string>
        <string>UIInterfaceOrientationLandscapeLeft</string>
        <string>UIInterfaceOrientationLandscapeRight</string>
    </array>
    <key>UIViewControllerBasedStatusBarAppearance</key>
    <false/>
    <key>NSLocationWhenInUseUsageDescription</key>
    <string></string>
  <key>NSAppTransportSecurity</key>
  <dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
  </dict>
</dict>
</plist>

对于真正的生产就绪解决方案,https://stackoverflow.com/a/36368360/5943130 该解决方案更好

For a real production ready solution, https://stackoverflow.com/a/36368360/5943130 that solution is better

这篇关于如果ssl(https)证书无效,则React Native XMLHttpRequest请求将失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆