我经常搜索,但不能很好地回答。 有一个最新版本的Alamofire 3.0发布。 我想知道如何设置客户端超时请求。 我在swift 3.0,Alamofire 3.0版本上尝试了这个,但它不起作用。 var alamoFireManager:Alamofire.Manager?
let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
configuration.timeoutIntervalForRequest = 4 // seconds
configuration.timeoutIntervalForResource = 4
self.alamoFireManager = Alamofire.Manager(configuration: configuration)
self.alamoFireManager!.request(.POST, "http://oznet.go.ro/iDorMobile/ConfigServer/api.php/timeout/2", parameters: nil).responseJSON {
(req, res, json, error) in
println("First json \(json)")
println("First error \(error)")
}
请帮帮我 在此先感谢
答案 0 :(得分:1)
您可以在http请求的标头中设置超时,如下例所示。
let request = NSMutableURLRequest(URL: NSURL.init(string: "URL")!)
request.HTTPMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.timeoutInterval = 10 // 10 secs
let values = ["key": "value"]
request.HTTPBody = try! NSJSONSerialization.dataWithJSONObject(values, options: [])
Alamofire.request(request).responseJSON {
response in
// do whatever you want here
}