我以前见过这个错误,似乎补救措施是向 .plist 文件添加信息。我已经尝试了我见过的每一个:
如何在 iOS 9 中启用应用传输安全性加载 HTTP URL?
NSURLSession/NSURLConnection HTTP 加载在 iOS 9 上失败
子域上的 NSURLSession/NSURLConnection HTTP 加载失败(kCFStreamErrorDomainSSL,-9802)?
我的程序仍然导致抛出相同的错误:
2017-05-08 08:29:37.781075-0400 xxxx[3256:408961] [] nw_coretls_callback_handshake_message_block_invoke_3 tls_handshake_continue: [-9812] 5-9812] 5-9818 2018730000000000000000000000000000000000000010000000000000000000000000000000201820201020820100000000000000000000000000000000000000000000000000001 kCFStreamErrorDomainSSL, -9813)
我的相关代码在这里:
let username = "xxxx"
let password = "xxxx"
let loginString = String(format: "%@:%@", username, password)
let loginData = loginString.data(using: String.Encoding.utf8)!
let base64LoginString = loginData.base64EncodedString()
let baseUrl = "https://xxxx"
var request = URLRequest(url: URL(string: baseUrl)! as URL)
let baseUrl = "server i want.xml"
let request = NSMutableURLRequest(url: NSURL(string: baseUrl)! as URL)
request.httpMethod = "POST"
request.setValue("Basic \(base64LoginString)", forHTTPHeaderField: "Authorization")
let session = URLSession.shared
var err: NSError?
let task = session.dataTask(with: request as URLRequest) {
(data, response, error) in
if data == nil {
print("dataTaskWithRequest error: \(err)")
return
}
let xml = SWXMLHash.parse(data!)
let serialNumFromXML = xml["mobile_devices"]["mobile_device"]["serial_number"].element?.text
func urlSession(
_ session: URLSession,
didReceive challenge: URLAuthenticationChallenge,
completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void)
{
completionHandler(
.useCredential,
URLCredential(trust: challenge.protectionSpace.serverTrust!))
}
我读过它可能与证书有关,但我无法更改服务器,因为我不负责它,所以我无法获得适当的证书。除了改变 plist 之外,还有更多吗?
我的 plist 文件具有允许任意加载和我的 XML 文件所在域的异常域。
我可以获取测试文件的内容,例如https://www.w3schools.com/xml/note.xml,但我需要的文件位于用户名/密码“需要身份验证”弹出窗口后面。
我也试过 Alamofire 库,同样的事情发生:
nw_coretls_callback_handshake_message_block_invoke_3 tls_handshake_continue: [-9807] 2017-05-08 09:42:08.452 xxx[6128:888398] NSURLSession/NSURLConnection HTTP 加载失败 (kCF9ErrorDomain)-SSL
好吧,我不知道如何解决这个问题,但我发现Alamofire可以用来做我需要的事情。这是一个用于库存管理的本地项目,所以我不担心绕过证书的安全性。我的代码在这里:
Manager.delegate.sessionDidReceiveChallenge = { session, challenge in
var disposition: URLSession.AuthChallengeDisposition = .performDefaultHandling
var credential: URLCredential?
if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust, let trust = challenge.protectionSpace.serverTrust {
disposition = URLSession.AuthChallengeDisposition.useCredential
credential = URLCredential(trust: trust)
} else {
if challenge.previousFailureCount > 0 {
disposition = .cancelAuthenticationChallenge
} else {
credential = Manager.session.configuration.urlCredentialStorage?.defaultCredential(for: challenge.protectionSpace)
if credential != nil {
disposition = .useCredential
}
}
}
return (disposition, credential)
}
private var Manager: Alamofire.SessionManager = {
// Create the server trust policies
let serverTrustPolicies: [String: ServerTrustPolicy] = [
"https://SERVER:PORT": .disableEvaluation
]
// Create custom manager
let configuration = URLSessionConfiguration.default
configuration.httpAdditionalHeaders = Alamofire.SessionManager.defaultHTTPHeaders
let manager = Alamofire.SessionManager(
configuration: URLSessionConfiguration.default,
serverTrustPolicyManager: ServerTrustPolicyManager(policies: serverTrustPolicies)
)
let sessionManager = SessionManager(
serverTrustPolicyManager: ServerTrustPolicyManager(
policies: ["https://SERVER:PORT": .disableEvaluation]
)
)
return manager
}()
open class MyServerTrustPolicyManager: ServerTrustPolicyManager {
// Override this function in order to trust any self-signed https
open override func serverTrustPolicy(forHost host: String) -> ServerTrustPolicy? {
return ServerTrustPolicy.disableEvaluation
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句