【发布时间】: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 也不起作用。
任何帮助将不胜感激。
【问题讨论】: