swift 加载失败,错误为Domain=NSURLErrorDomain Code=-999“cancelled”

wtlkbnrh  于 2023-09-30  发布在  Swift
关注(0)|答案(2)|浏览(193)

我来拿
“加载失败,错误为Error Domain=NSURLErrorDomain Code=-999“cancelled””
在我的自定义框架中尝试调用API时。但是当我在另一个项目中运行相同的代码时,它工作得很好,并给予了正确的响应。下面是我的代码。有没有什么我必须做的自定义框架,不需要在正常的项目。

func invokePostWebServiceCallLogin(request : String,param : NSDictionary,completion : @escaping (_ webResponse : WebserviceResponseClass) ->Void) -> Void {

        let headers = [
            "Accept": "application/json",
            "Content-Type": "application/x-www-form-urlencoded"
        ]
        let passingParameter : [ String : AnyObject] = param as! [String : AnyObject]

        UIApplication.shared.isNetworkActivityIndicatorVisible = true

        WebServiceHelperClass.Manager.request(URL(string: request)!, method: .post, parameters: passingParameter,encoding: URLEncoding.default, headers: headers).validate().responseJSON {
            response in
            UIApplication.shared.isNetworkActivityIndicatorVisible = false

            switch response.result {
            case .success:
                let webResult =  WebserviceResponseClass()
                webResult.isSuccess = true
                webResult.responseData = response.data as NSData?
                webResult.error = nil
                completion(webResult)
                break
            case .failure(let error):
                let webResult =  WebserviceResponseClass()
                webResult.isSuccess = false
                webResult.responseData = nil
                webResult.error = error as NSError
                completion(webResult)
            }
        }
    }

    private static var Manager : Alamofire.SessionManager = {
         let    serverTrustPolicies: [String: ServerTrustPolicy] = [
                "dev.xxxxx.com": .pinCertificates(
                    certificates: ServerTrustPolicy.certificates(),
                    validateCertificateChain: true,
                    validateHost: true
            ),
                "xx.xx.xxx.xx": .disableEvaluation
            ]

        let configuration = URLSessionConfiguration.default
        configuration.timeoutIntervalForRequest = 45
        configuration.timeoutIntervalForResource = 45

        let manager = Alamofire.SessionManager(
            configuration: configuration,
            serverTrustPolicyManager: ServerTrustPolicyManager(policies: serverTrustPolicies)
        )
        return manager
    }

Task<91C7555E-F6E6-45EB-9762-EE61915719DE>. <1>load failed with error Domain=NSURLErrorDomain Code=-999“cancelled”UserInfo={NSErrorFailingURLStringKey=,NSErrorFailingURLKey=,_NSURLErrorRelatedURLSessionTaskErrorKey=(“LocalDataTask<91C7555E-F6E6-45EB-9762-EE61915719DE>.<1>“),_NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask<91C7555E-F6E6-45EB-9762-EE61915719DE>.<1>,NSLocalizedDescription=cancelled} [-999]

kulphzqa

kulphzqa1#

看起来API调用正在使用证书固定安全策略。当证书固定失败时,数据任务将返回NSLocalizedDescription=cancelled。检查ServerTrustPolicy.certificates()并查看它是否返回有效的证书数据-通常它会自动加载SAME捆绑包中的任何证书。如果没有,请确保手动加载

s2j5cfk0

s2j5cfk02#

我遇到了同样的问题,发现我的新证书没有被ServerTrustPolicy.certificates()加载。原来我需要DER(Distinguished Encoding Rules)编码的证书,我有PEM证书。
要将PEM转换为DER,我使用以下命令并成功添加了新证书。

openssl x509 -outform der -in certificatename.pem -out certificatename.der

相关问题