问题
The service I'm connecting to is using a self signed certificate. For dev purposes I do not want to validate that chain.
Using swift 3 with Alamofire 4. Fixed the ATS accordingly:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>url.com</key>
<dict>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSExceptionRequiresForwardSecrecy</key>
<false/>
<key>NSIncludesSubdomains</key>
<true/>
</dict>
</dict>
</dict>
Code to connect and disable evaluation.
let serverTrustPolicies: [String: ServerTrustPolicy] = [
"example.domain.com": .pinCertificates(
certificates: ServerTrustPolicy.certificates(),
validateCertificateChain: false,
validateHost: true
),
"sub.url.com": .disableEvaluation
]
let sessionManager = Alamofire.SessionManager(
serverTrustPolicyManager: ServerTrustPolicyManager(policies: serverTrustPolicies)
)
let headers = ["Authorization": "Basic /*...*/"]
sessionManager.request("https://sub.url.com/path", headers: headers).responseJSON { response in
print(response.request) // original URL request
print(response.response) // HTTP URL response
print(response.data) // server data
print(response.result) // result of response serialization
debugPrint(response)
if let JSON = response.result.value {
print("JSON: \(JSON)")
}
}
Error log from dumpPrint
[Result]: FAILURE: Error Domain=NSURLErrorDomain Code=-999 "cancelled" UserInfo={NSErrorFailingURLKey=https://sub.url.com/path, NSLocalizedDescription=cancelled, NSErrorFailingURLStringKey=https://sub.url.com/path}
URL has been masked.
回答1:
To retain SessionManager
instance you need to capture it in closure passed to responseJSON
:
sessionManager.request("https://sub.url.com/path", headers: headers).responseJSON { response in
let _ = sessionManager // retain
// ...
}
Otherwise sessionManager
is deallocated shortly it goes out of scope and any executing requests are cancelled.
回答2:
Please add this statement to the end of responseJson block:
manager.session.invalidateAndCancel()
It happens if the object of the manager is not retained till execution of the block completes, so this would ensure its retention.
Cheers!
回答3:
Please check in sessiondidReceiveChallenge:
delegate implementation of NSURLSession
. Chances are NSURLSessionAuthChallengeCancelAuthenticationChallenge
is getting executed somewhere.
回答4:
self.getSessionManager().request(urlstring, method: methods, parameters: parameters, encoding: JSONEncoding.prettyPrinted, headers: Header).responseJSON(queue: nil, options: JSONSerialization.ReadingOptions.allowFragments) { (responseObject) in ... .... }.session.finishTasksAndInvalidate()
Just put the method of invalidate after task finish, means session.finishTasksAndInvalidate()
回答5:
You need to properly manage the lifetime of your SessionManager
instance. Most commonly this is done by making a singleton instance. Otherwise, when your manager goes out of scope and is deinit
ed, all outstanding requests will be cancelled.
来源:https://stackoverflow.com/questions/39984880/alamofire-result-failure-error-domain-nsurlerrordomain-code-999-cancelled