ios - 此服务器的证书无效。您可能正在连接到伪装成 “domain” 的服务器

标签 ios swift ssl ssh server

我正在尝试从 URL 中获取 JSON 但无法这样做,因为发生了错误,因此响应和数据为零。

我从 URL 获取数据的代码

 loadData(urlString: String, completion : @escaping (Player,Player) -> Void) -> Void {
    let config = URLSessionConfiguration.default

    let session = URLSession(configuration: config)

    let url = URL(string: urlString)
    let task = session.dataTask(with: url!){data,response,error in
        if error != nil{
            print("problem in loading data");
            print(error.debugDescription);
        } else{
            let players = self.parseJson(data: data as NSData?)

            let priority = DispatchQueue.global(qos: .userInitiated)
            priority.async {
                DispatchQueue.main.async {
                    completion(players.0, players.1);
                }
            }
        }
    }
    task.resume();

错误的Debug描述如下,在info.plist中有App Transport Security的详细信息—— Image showing info.plist and error.debugDescription

以及我用于从 JSON URL 获取数据的代码图像。 enter image description here

最佳答案

使用 NSURLSessionDelegate

enter image description here

您需要添加适当的 session 配置

enter image description here 实现 - (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler

enter image description here

关于ios - 此服务器的证书无效。您可能正在连接到伪装成 “domain” 的服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40763967/

相关文章:

ios - 如何在tableView中进行分页?

ios - Swift UITextField 类不显示编辑

c++ - Cyassl-2.4.2 缺少 libcyassl.a 文件

ssl - 可以强制Firefox使用ssl而不是tls

swift - 大整数 Swift 4.0

objective-c - 使用变量填充 UITableView

ios - 如何从 Firestore 将包含对象的数组获取到 Xcode 中的模型

android - 我可以将 c++ 库包装到移动 IOS/Android 的 Adob​​e Air native 扩展吗

java - 仅使用 CA 在 Java 中使用 gRPC 对服务器执行客户端身份验证

android - Cordova CLI "cordova build android"错误 "Does not appear to be an xcode project, no xcode project file in..."?