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

The certificate for this server is invalid ( error: 9813 )(此服务器的证书无效(错误:9813))

本文介绍了此服务器的证书无效(错误:9813)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试连接到我的ASP.NET Core API,该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()

我目前不关心任何风险,因为这只是为了开发目的。是否有方法可以信任连接或完全忽略检查?

推荐答案

我终于想明白了。

我在我的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!))
    }

}

这篇关于此服务器的证书无效(错误:9813)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:此服务器的证书无效(错误:9813)

基础教程推荐