简体   繁体   English

Swift:如何使用服务器SSL证书进行Https请求

[英]Swift: How to Make Https Request Using Server SSL Certificate

Hi I want to make Https Request in Swift. 嗨,我想在Swift中进行Https请求。 Currently im accessing local server through ip address. 当前即时通讯通过IP地址访问本地服务器。 Local Server has one SSL Certificate by accessing the certificate want to make request to server currently im doing like this. 本地服务器通过访问要向当前正在这样做的服务器发出请求的证书来获得一个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)")


            }
    }

I have used the above code for making request and in plist 我已经使用上面的代码进行请求并在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>

in plist i have given like this but im still getting error like 在plist中,我已经给出了这样的信息,但我仍然收到类似的错误

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

IMPORTANT: DO NOT USE THIS IN PRODUCTION. 重要说明:请勿在生产中使用此产品。 Basically, with Alamofire, you can bypass the authentication for app development and testing purpose. 基本上,使用Alamofire,您可以绕过身份验证以进行应用程序开发和测试。 Make sure you remove it before the app is on the App Store or in production:- 确保在应用程序在App Store或正式生产中之前将其删除:-

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. 让我知道是否有帮助。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Swift绕过HTTPS服务器请求 - How to bypass HTTPS server request using swift Swift 3如何使用SSL Pinning和AlamoFire验证服务器证书? - Swift 3 How to validate server certificate using SSL Pinning and AlamoFire? 如何使用有效的SSL证书通过iOS中的AFNetworking建立https连接? - How to use valid SSL certificate to make https connection using AFNetworking in iOS? 用于在iOS上运行HTTPS服务器的SSL身份证书 - SSL Identity Certificate to run an HTTPS Server on iOS Swift 2 - Xcode 7.0无法使用不受信任的SSL证书访问HTTPS站点 - Swift 2 - Xcode 7.0 Cannot Access HTTPS site with unstrusted SSL Certificate 如何在 swift 中发出 REST API 请求,并以字符串形式提供证书? - How to make a REST API request, with a certificate provided as String, in swift? 如何快速通过证书向REST API请求? - How do I make a request to a REST API, with a certificate, in swift? 如果ssl(https)证书无效,则React Native XMLHttpRequest请求将失败 - React Native XMLHttpRequest request fails if ssl (https) certificate is not valid 在Swift中使用NSURLConnection和有效的SSL证书 - Using NSURLConnection with a valid SSL certificate in Swift iOS应用程序无法通过GoDaddy SSL证书与HTTPS服务器通信 - IOS application failing with communicating with HTTPS server with GoDaddy SSL certificate
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM