该服务器的证书无效(错误:9813)

时间:2019-11-29 15:17:33

标签: swift nsurlsession

我正在尝试连接到我在另一台计算机上运行的ASP.NET Core API。我想尝试使用POST请求添加数据。我收到以下错误消息:

连接6:默认TLS信任评估失败(-9813)

连接6:TLS信任遇到错误3:-9813

连接6:遇到错误(3:-9813)

错误描述是:

此服务器的证书无效。您可能正在连接到假装为“ 192.168.0.100”的服务器,这可能会使您的机密信息受到威胁。

let jsonData = try? JSONSerialization.data(withJSONObject: data)

let url = URL(string: "https://192.168.0.100:5001/api/Trips")!

var request = URLRequest(url: url)
request.httpMethod = "POST"
request.httpBody = jsonData

let task = URLSession.shared.dataTask(with: request) { data, response, error in
    guard let data = data, error  == nil else {
        print(error?.localizedDescription)
        return
    }

    let responseJSON = try? JSONSerialization.jsonObject(with: data, options: [])

    if let responseJSON = responseJSON as? [String: Any] {

    }

}

task.resume()

我目前不担心任何风险,因为这只是出于发展目的。有没有办法信任连接或完全忽略检查?

2 个答案:

答案 0 :(得分:2)

您需要在info.plist中设置这些属性

class MyBot {

    // constructor...

    async onTurn(context) {
        // ...
        var search_engine_answer = await pysearch.searchForRelevantDoc(context);
        context.sendActivity(search_engine_answer)
        // ...
    }
}

答案 1 :(得分:0)

我终于明白了。

我在我的info.plist中添加了以下几行: INFO.PLIST

我使用以下设置创建了会话对象:

let session = URLSession(configuration: URLSessionConfiguration.default, delegate: self, delegateQueue: OperationQueue.main)

然后将此扩展名添加到代码的底部:

extension MyViewController : URLSessionDelegate {

    func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
        completionHandler(.useCredential, URLCredential(trust: challenge.protectionSpace.serverTrust!))
    }

}

出于安全考虑,请不要忘记在部署应用程序时将其删除。

我希望我能帮助别人。感谢大家的建议。这是我的代码现在的样子:

import UIKit

class MyViewController: UIViewController {

    @IBOutlet weak var createButton: UIBarButtonItem!

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    @IBAction func createButtonTapped(_ sender: Any) {

        let data: [String: Any] = ["data1": data1, "data2": data2......]

        let jsonData = try? JSONSerialization.data(withJSONObject: data)

        let session = URLSession(configuration: URLSessionConfiguration.default, delegate: self, delegateQueue: OperationQueue.main)

        let url = URL(string: "https://192.168.0.100:5001/api/Trips")!

        var request = URLRequest(url: url)
        request.httpMethod = "POST"
        request.httpBody = jsonData
        request.addValue("application/json",forHTTPHeaderField: "Content-Type")
        request.addValue("application/json",forHTTPHeaderField: "Accept")

        let task = session.dataTask(with: request) { data, response, error in
            guard let data = data, error  == nil else {
                print(error?.localizedDescription)
                return
            }

            let responseJSON = try? JSONSerialization.jsonObject(with: data, options: [])

            if let responseJSON = responseJSON as? [String: Any] {
                .....
            }

        }

        task.resume()
    }
}


extension MyViewController : URLSessionDelegate {

    func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
        completionHandler(.useCredential, URLCredential(trust: challenge.protectionSpace.serverTrust!))
    }

}