Swift 3.0 Alamofire 4.0 - Domain=NSURLErrorDomain Code=-999 “取消”
Posted
技术标签:
【中文标题】Swift 3.0 Alamofire 4.0 - Domain=NSURLErrorDomain Code=-999 “取消”【英文标题】:Swift 3.0 Alamofire 4.0 - Domain=NSURLErrorDomain Code=-999 "cancelled" 【发布时间】:2016-11-26 14:09:32 【问题描述】:我正在尝试将 Alamofire(最新版本)与 Swift 3.0、ios 10 和 xCode 8 一起使用。目前正在尝试进行后期通话。代码写在下面,我一直遇到同样的问题。请提供帮助,对此不胜感激。
private class func getDefaultHeaders() -> HTTPHeaders
let headers: HTTPHeaders = [
"Connection" : "keep-alive",
"token" : "0781d3957fd8da6ee35c4e3124d974a2999925274771234",
"nonce" : "9b2436331ed908bb2f399568d2adbc4e",
"uuid" : "uuid",
"latitude" : "43.656781",
"longitude" : "-79.380823",
"Content-Type" : "application/json",
"userTypeId" : "1",
"userAgent" : "UserAgent",
"Content-Length" : "0",
"Host" : "localhost:8080",
"User-Agent" : "iOS Example/1.0 (com.alamofire.iOS-Example; build:1; iOS 10.0.0) Alamofire/4.0.0"
]
return headers
func generateCustomSession() -> SessionManager
let headers = ServicesController.getDefaultHeaders()
let configuration = URLSessionConfiguration.default
configuration.httpAdditionalHeaders = headers
let manager: SessionManager = SessionManager(configuration: configuration)
return manager
func post(url: String, success:@escaping (JSON) -> Void, failure:@escaping (Error) -> Void)
generateCustomSession().request(
url,
method: .post,
encoding: JSONEncoding.default)
.validate(statusCode: 200..<300)
.downloadProgress (progress) -> Void in
print("download progress: \(progress.fractionCompleted)")
.responseJSON (response) -> Void in
if #available(iOS 10.0, *)
print(response.metrics)
debugPrint(response)
if response.result.isSuccess
let resJson = JSON(response.result.value!)
success(resJson)
if response.result.isFailure
let error : Error = response.result.error!
failure(error)
【问题讨论】:
【参考方案1】:如果可疑的 https 证书存在问题,可以由允许访问的用户修复,但这种情况不会发生,则可能会发生“取消”。
很可能根本原因是服务器使用的证书不够完善。
【讨论】:
除了告诉服务器修复您的证书问题之外,是否应该有解决此问题的方法。【参考方案2】:我找到了解决问题的方法。下面是帮助您取消身份验证质询的代码。我不推荐这个,这对于临时解决方案很有用。最好的办法仍然是完全验证身份验证质询。
let manager: SessionManager = SessionManager(configuration: configuration, serverTrustPolicyManager: ServerTrustPolicyManager(policies: serverTrustPolicies))
manager.delegate.sessionDidReceiveChallenge = session, challenge in
var disposition: URLSession.AuthChallengeDisposition = .cancelAuthenticationChallenge
var credential: URLCredential?
if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust
disposition = URLSession.AuthChallengeDisposition.useCredential
credential = URLCredential(trust: challenge.protectionSpace.serverTrust!)
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)
return manager
【讨论】:
以上是关于Swift 3.0 Alamofire 4.0 - Domain=NSURLErrorDomain Code=-999 “取消”的主要内容,如果未能解决你的问题,请参考以下文章
Alamofire 4.0 / Swift 3.0 - 附加多部分表单数据(CSV 文件)
在 Swift 3.0 (Alamofire 4.4.0) 中的一些请求后,Alamofire 停止工作
Swift 3.0 迁移后的 Alamofire 错误:“调用中的额外参数”(请求方法)