繁体   English   中英

错误域= NSURLErrorDomain代码= -999“已取消” Xcode

[英]Error Domain=NSURLErrorDomain Code=-999 “cancelled” Xcode

我正在尝试向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 = cancelled,NSErrorFailingURLKey = https:// domain / WebApiMobileGateway / api / MobileGatewayApi / PreActivation

我尝试将域添加到Info.plist NSAppTransportSecurity中,但这没有用。 开启NSAllowsArbitraryLoads也不起作用。

任何帮助,将不胜感激。

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

变更条件如下

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM