簡體   English   中英

Swift:如何使用服務器SSL證書進行Https請求

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

嗨,我想在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)

重要說明:請勿在生產中使用此產品。 基本上,使用Alamofire,您可以繞過身份驗證以進行應用程序開發和測試。 確保在應用程序在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)
        }
    }

謝謝! 讓我知道是否有幫助。 :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM