Alamofire.request(.GET,"https://host.com/a/path").authenticate(user: "user",password: "typo") .responseString { (req,res,data,error) in if error != nil { println("STRING Error:: error:\(error)") println(" req:\(req)") println(" res:\(res)") println(" data:\(data)") return } println("SUCCESS for String")} .responseJsON { (req,error) in if error != nil { println("JsON Error:: error:\(error)") println(" req:\(req)") println(" res:\(res)") println(" data:\(data)") return } println("SUCCESS for JsON")}
不幸的是,产生的错误似乎并不表示实际接收到http状态代码409:
STRING Error:: error:Optional(Error Domain=NSURLErrorDomain Code=-999 "cancelled" UserInfo=0x7f9beb8efce0 {NSErrorFailingURLKey=https://host.com/a/path,NSLocalizedDescription=cancelled,NSErrorFailingURLStringKey=https://host.com/a/path}) req:<NSMutableURLRequest: 0x7f9beb89d5e0> { URL: https://host.com/a/path } res:nil data:Optional("")JsON Error:: error:Optional(Error Domain=NSURLErrorDomain Code=-999 "cancelled" UserInfo=0x7f9beb8efce0 {NSErrorFailingURLKey=https://host.com/a/path,NSErrorFailingURLStringKey=https://host.com/a/path}) req:<NSMutableURLRequest: 0x7f9beb89d5e0> { URL: https://host.com/a/path } res:nil data:nil
另外,当发生错误时检索http主体是很好的,因为我的服务器端会在那里放置一个错误的文本描述。
问题
是否可以在非2xx响应时检索状态代码?
是否可以在2xx响应时检索特定的状态代码?
是否可以在非2xx响应时检索http主体?
谢谢!
对于Alamofire> = 4.0的Swift 3.x用户Alamofire.request(urlString) .responseString { response in print("Success: \(response.result.isSuccess)") print("Response String: \(response.result.value)") 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(statusCode) // the status code }
(Alamofire 4包含一个全新的错误系统,查看here的详细信息)
对于Alamofire> = 3.0的Swift 2.x用户
Alamofire.request(.GET,urlString) .responseString { response in print("Success: \(response.result.isSuccess)") print("Response String: \(response.result.value)") if let alamoError = response.result.error { let alamoCode = alamoError.code let statusCode = (response.response?.statusCode)! } else { //no errors let statusCode = (response.response?.statusCode)! //example : 200 }}总结
以上是内存溢出为你收集整理的Swift Alamofire:如何获取HTTP响应状态代码全部内容,希望文章能够帮你解决Swift Alamofire:如何获取HTTP响应状态代码所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)