Alamofire 4 FAILURE使用Swift 3.0:错误域= NSURLErrorDomain代码= -999“已取消”

时间:2016-11-22 05:51:12

标签: ios https request swift3 alamofire

我正在使用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,不建议将bypassURLA认证用于PRODUCTION。所以我需要帮助来解决这个问题。

3 个答案:

答案 0 :(得分:0)

使用它: -

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

            }


}

答案 1 :(得分:0)

您需要保留对defaultManager实例的引用。否则,会话管理器在返回响应之前被取消分配。有关详细信息,请参阅此处:https://github.com/Alamofire/Alamofire/issues/157

答案 2 :(得分:0)

为您的班级制作 defaultManager 全局属性。