Swift:如何使用服务器SSL证书进行Https请求 [英] Swift: How to Make Https Request Using Server SSL Certificate

查看:255
本文介绍了Swift:如何使用服务器SSL证书进行Https请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我想在Swift中发出Https请求。目前我通过IP地址访问本地服务器。本地服务器有一个SSL证书,通过访问证书,想要向服务器发出请求,目前我这样做。

Hi I want to make Https Request in Swift. Currently im accessing local server through ip address. Local Server has one SSL Certificate by accessing the certificate want to make request to server currently im doing like this.

  Alamofire.request(.GET, https://ipaddress//, parameters: [param], headers: headers)
        .responseJSON { response in
            print(response.request)  // original URL request
            print(response.response) // URL response
            print(response.data)     // server data
            print(response.result)   // result of response serialization

            if let JSON = response.result.value {
                print("JSON: \(JSON)")


            }
    }

我使用上面的代码发出请求并在plist中

I have used the above code for making request and in plist

 <key>NSAppTransportSecurity</key>
 <dict>
  <key>NSExceptionDomains</key>
  <dict>
        <key>192.168.2.223:1021(my local ip)</key>
        <dict>
        Include to allow subdomains-->
            <key>NSIncludesSubdomains</key>
            <true/>
           <!--Include to allow insecure HTTP requests-->
           <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
            <true/>
            <!--Include to specify minimum TLS version-->
           <key>NSTemporaryExceptionMinimumTLSVersion</key>
            <string>TLSv1.1</string>
        </dict>
    </dict>
</dict>

在plist我已经给出了这样的但是我仍然得到错误,如

in plist i have given like this but im still getting error like

NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9813)


推荐答案

重要提示:请勿在生产中使用。基本上,使用Alamofire,您可以绕过应用程序开发和测试目的的身份验证。确保在应用程序位于App Store或生产中之前将其删除: -

IMPORTANT: DO NOT USE THIS IN PRODUCTION. Basically, with Alamofire, you can bypass the authentication for app development and testing purpose. Make sure you remove it before the app is on the App Store or in production:-

func bypassURLAuthentication() {
        let manager = Alamofire.Manager.sharedInstance
        manager.delegate.sessionDidReceiveChallenge = { session, challenge in
            var disposition: NSURLSessionAuthChallengeDisposition = .PerformDefaultHandling
            var credential: NSURLCredential?
            if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust {
                disposition = NSURLSessionAuthChallengeDisposition.UseCredential
                credential = NSURLCredential(forTrust: challenge.protectionSpace.serverTrust!)
            } else {
                if challenge.previousFailureCount > 0 {
                    disposition = .CancelAuthenticationChallenge
                } else {
                    credential = manager.session.configuration.URLCredentialStorage?.defaultCredentialForProtectionSpace(challenge.protectionSpace)
                    if credential != nil {
                        disposition = .UseCredential
                    }
                }
            }
            return (disposition, credential)
        }
    }

谢谢!
如果有帮助,请告诉我。 :)

Thank You! Let me know if this helps. :)

这篇关于Swift:如何使用服务器SSL证书进行Https请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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