【问题标题】:Error Domain=NSURLErrorDomain Code=-999 "cancelled" Xcode错误域=NSURLErrorDomain 代码=-999 “取消” Xcode
【发布时间】:2018-12-11 11:44:11
【问题描述】:

我正在尝试向 https 但没有 SSL 的服务器发出请求。

我试图在下面的代码中忽略 ssl 证书验证:

func loadInfoAny(parameters: parameters, url: "https://domain/WebApiMobileGateway/api/MobileGatewayApi/PreActivation", completionHandler:@escaping (Bool, [[String: Any]]?) -> ()) {
    let headers: HTTPHeaders = [
        "Content-Type": "application/json"
    ]


    let sessionManager = SessionManager()
    sessionManager.delegate.taskDidReceiveChallengeWithCompletion = { (_, _, challenge, completionHandler) -> Void in
        // Pass test server with self signed certificate
        if challenge.protectionSpace.host == url {
            completionHandler(.useCredential, URLCredential(trust: challenge.protectionSpace.serverTrust!))
        } else {
            completionHandler(.performDefaultHandling, nil)
        }
    }

    sessionManager.request(url, method:.post, parameters: parameters, headers : headers)
        .validate()
        .responseJSON { response in

            switch response.result {

            case let .success(value):

                 print(value)

            case let .failure(error):
                print(error)
                completionHandler(false, nil)
            }
    }

}

但我收到此错误:

错误域=NSURLErrorDomain 代码=-999 “已取消” UserInfo={NSErrorFailingURLStringKey=https://domain/WebApiMobileGateway/api/MobileGatewayApi/PreActivation, NSLocalizedDescription=取消, NSErrorFailingURLKey=https://domain/WebApiMobileGateway/api/MobileGatewayApi/PreActivation}。

我尝试在Info.plist 中将域添加到我的NSAppTransportSecurity,但没有奏效。打开NSAllowsArbitraryLoads 也不起作用。

任何帮助将不胜感激。

【问题讨论】:

    标签: swift xcode ssl


    【解决方案1】:

    问题是您正在将主机与网址进行比较。

    改变条件如下

     if challenge.protectionSpace.host == "41.230.10.77" {
         completionHandler(.useCredential, URLCredential(trust: challenge.protectionSpace.serverTrust!))
     } else {
         completionHandler(.performDefaultHandling, nil)
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-19
      • 2021-10-24
      • 2017-03-14
      • 2020-02-22
      • 2019-04-22
      • 1970-01-01
      • 2015-11-14
      • 2013-04-11
      相关资源
      最近更新 更多