Alamofire 仅针对特定请求设置超时
Alamofire set timeout only for specific a request
我正在使用 Alamofire 发送请求,如下所示。我需要知道如何只为这样的特定请求设置超时
Alamofire.request(URL, method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: headers).validate().responseJSON { (response) in switch response.result {
////
}
尝试为该特定请求创建一个 sessionManager
let sessionManager: SessionManager = {
var sessionManager = SessionManager()
let configuration = URLSessionConfiguration.default
configuration.timeoutIntervalForRequest = 30 //timeout can be changed from here
sessionManager = Alamofire.SessionManager(configuration: configuration)
return sessionManager
}()
并使用此 sessionManager
进行通话。
sessionManager.request(url, method: .get, parameters: params)...
对于特定的 url 或请求超时使用 timeoutInterval
。
if let url = NSURL.init(string: "YOUR_URL") {
var request = URLRequest(url: url as URL)
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
if let myUrl = NSURL.init(string: "SPECIFIC_URL"),
myUrl.isEqual(url) {
request.timeoutInterval = 10 // 10 secs
}
let postString = "param1=\(var1)¶m2=\(var2)"
request.httpBody = postString.data(using: .utf8)
Alamofire.request(request).responseJSON { response in
/// handle response here
}
}
根据 git hub validate() 检查状态代码是否在 200..<300
范围内,并且响应的 Content-Type
header 与 [=请求的 14=] header,如果提供的话。
我正在使用 Alamofire 发送请求,如下所示。我需要知道如何只为这样的特定请求设置超时
Alamofire.request(URL, method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: headers).validate().responseJSON { (response) in switch response.result {
////
}
尝试为该特定请求创建一个 sessionManager
let sessionManager: SessionManager = {
var sessionManager = SessionManager()
let configuration = URLSessionConfiguration.default
configuration.timeoutIntervalForRequest = 30 //timeout can be changed from here
sessionManager = Alamofire.SessionManager(configuration: configuration)
return sessionManager
}()
并使用此 sessionManager
进行通话。
sessionManager.request(url, method: .get, parameters: params)...
对于特定的 url 或请求超时使用 timeoutInterval
。
if let url = NSURL.init(string: "YOUR_URL") {
var request = URLRequest(url: url as URL)
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
if let myUrl = NSURL.init(string: "SPECIFIC_URL"),
myUrl.isEqual(url) {
request.timeoutInterval = 10 // 10 secs
}
let postString = "param1=\(var1)¶m2=\(var2)"
request.httpBody = postString.data(using: .utf8)
Alamofire.request(request).responseJSON { response in
/// handle response here
}
}
根据 git hub validate() 检查状态代码是否在 200..<300
范围内,并且响应的 Content-Type
header 与 [=请求的 14=] header,如果提供的话。