使用 Swift 3.0 的 Alamofire 4 失败:错误域 = NSURLErrorDomain 代码 = -999 “已取消”
Posted
技术标签:
【中文标题】使用 Swift 3.0 的 Alamofire 4 失败:错误域 = NSURLErrorDomain 代码 = -999 “已取消”【英文标题】:Alamofire 4 FAILURE With Swift 3.0 : Error Domain=NSURLErrorDomain Code=-999 “cancelled” 【发布时间】:2016-11-22 05:51:11 【问题描述】:我正在使用 Alamofire 进行 https 连接。
但是在 Alamofire 中进行测试时,我得到了这个错误:
URLError occurred: URLError(_nsError: Error Domain=NSURLErrorDomain Code=-999 "cancelled" UserInfo=NSErrorFailingURLKey=https://192.168.X.XX/VMSSite/Handlers/Common/iosServer.ashx?RegID=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX, NSLocalizedDescription=cancelled, NSErrorFailingURLStringKey=https://192.168.X.XX/VMSSite/Handlers/Common/iOSServer.ashx?RegID=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX) status code: nil
我的来源是这样的:
var defaultManager = Alamofire.SessionManager.default
let serverTrustPolicies: [String: ServerTrustPolicy] = [
"192.168.X.XX:443": .disableEvaluation,
"192.168.X.XX": .disableEvaluation
]
let configuration = URLSessionConfiguration.default
configuration.timeoutIntervalForRequest = 30 //seconds
configuration.httpAdditionalHeaders = Alamofire.SessionManager.defaultHTTPHeaders
//defaultManager = SessionManager(configuration: configuration, delegate: SessionDelegate(), serverTrustPolicyManager: ServerTrustPolicyManager(policies: serverTrustPolicies))
defaultManager = SessionManager(configuration: configuration, serverTrustPolicyManager: ServerTrustPolicyManager(policies: serverTrustPolicies))
defaultManager.request("https://192.168.X.XX/VMSSite/Handlers/Common/iOSServer.ashx?RegID=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", method: .post, parameters: nil, encoding: JSONEncoding.default).responseJSON response in
debugPrint(response)
var statusCode = response.response?.statusCode
if let error = response.result.error as? AFError
statusCode = error._code // statusCode private
switch error
case .invalidURL(let url):
print("Invalid URL: \(url) - \(error.localizedDescription)")
case .parameterEncodingFailed(let reason):
print("Parameter encoding failed: \(error.localizedDescription)")
print("Failure Reason: \(reason)")
case .multipartEncodingFailed(let reason):
print("Multipart encoding failed: \(error.localizedDescription)")
print("Failure Reason: \(reason)")
case .responseValidationFailed(let reason):
print("Response validation failed: \(error.localizedDescription)")
print("Failure Reason: \(reason)")
switch reason
case .dataFileNil, .dataFileReadFailed:
print("Downloaded file could not be read")
case .missingContentType(let acceptableContentTypes):
print("Content Type Missing: \(acceptableContentTypes)")
case .unacceptableContentType(let acceptableContentTypes, let responseContentType):
print("Response content type: \(responseContentType) was unacceptable: \(acceptableContentTypes)")
case .unacceptableStatusCode(let code):
print("Response status code was unacceptable: \(code)")
statusCode = code
case .responseSerializationFailed(let reason):
print("Response serialization failed: \(error.localizedDescription)")
print("Failure Reason: \(reason)")
// statusCode = 3840 ???? maybe..
print("Underlying error: \(error.underlyingError)")
else if let error = response.result.error as? URLError
print("URLError occurred: \(error)")
else
print("Unknown error: \(response.result.error)")
print("status code: \(statusCode)") // the status code
我正在使用来自 xcode8.1 分支的 Swift 3 和 Alamofire 4.0。 请帮帮我。
和
如何在 ATS 中为动态主机名设置异常域的值?
注意:如果我在此代码中使用 bypassURLAuthentication,则它与 https 一起工作正常。但根据帖子here bypassURLAuthentication 不建议用于生产。所以我需要帮助来解决这个问题。
【问题讨论】:
见***.com/a/42985488/746347 【参考方案1】:使用这个:-
Alamofire.request("https://192.168.X.XX/VMSSite/Handlers/Common/iOSServer.ashx?RegID=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", method: .post, parameters: nil, headers: nil).responseJSON (response:DataResponse<Any>) in
switch(response.result)
case .success(_):
if let data = response.result.value
print("your response data \(response.data!)")
break
case .failure(_):
print(response.result.error)
break
【讨论】:
我试过这个但是想用 SSL (serverTrustPolicies) 发出请求,这就是为什么我不能使用 Alamofire.request 如果你看到我的代码我使用 sessionmanager 调用请求,我收到了这个错误.【参考方案2】:您需要保留对您的 defaultManager 实例的引用。否则,会话管理器在返回响应之前被释放。更多信息请看这里:https://github.com/Alamofire/Alamofire/issues/157
【讨论】:
【参考方案3】:为您的班级创建 defaultManager 全局属性。
【讨论】:
以上是关于使用 Swift 3.0 的 Alamofire 4 失败:错误域 = NSURLErrorDomain 代码 = -999 “已取消”的主要内容,如果未能解决你的问题,请参考以下文章
如何使用参数 swift 3.0 Alamofire 4.0 调用邮政服务?
如何使用 Alamofire 在 Xcode 8 Swift 3.0 中获取特定的 JSON?