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

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Why local notification is not firing for UNCalendarNotificationTrigger(为什么没有为UNCalendarNotificationTrigger触发本地通知)
Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
Dropbox Files.download does not start when number of files in folder is gt; 1000(当文件夹中的文件数为1000时,Dropbox Files.Download不会启动)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
Get an event when UIBarButtonItem menu is displayed(显示UIBarButtonItem菜单时获取事件)
iOS: Send multiple actions to a bar button(IOS:向一个栏按钮发送多个动作)