【问题标题】:Swift: How to Make Https Request Using Server SSL CertificateSwift:如何使用服务器 SSL 证书发出 Https 请求
【发布时间】:2016-07-06 00:58:01
【问题描述】:

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

  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 中

 <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 中我已经给出了这样的结果,但我仍然收到类似的错误

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

【问题讨论】:

  • 您不能在 ATS plist 值中使用 IP 地址。
  • 你找到解决方案了吗?

标签: ios swift ssl https


【解决方案1】:

重要提示:请勿在生产中使用它。基本上,使用 Alamofire,您可以绕过身份验证以进行应用程序开发和测试。确保在应用上架或投入生产之前将其删除:-

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)
        }
    }

谢谢! 让我知道这是否有帮助。 :)

【讨论】:

    猜你喜欢
    • 2015-08-27
    • 1970-01-01
    • 2015-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多