Tôi đang làm việc trên một dự án Swift đòi hỏi nhiều tiêu thụ API. Mọi thứ đều hoạt động tốt nhưng đôi khi (1 trong 20), tôi nhận được lỗi Code=-1001 "The request timed out."
trong khi gọi API.Mã = -1001 "Yêu cầu đã hết thời gian chờ".
Tôi đang sử dụng Alamofire. Tôi đang gắn mã để gọi API.
let request = NSMutableURLRequest(URL: url)
request.HTTPMethod = "POST"
request.HTTPBody = myUrlContents.dataUsingEncoding(NSUTF8StringEncoding)
request.timeoutInterval = 15
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.setValue("application/json", forHTTPHeaderField: "Accept")
request.setValue("\(myUrlContents.dataUsingEncoding(NSUTF8StringEncoding)!.length)", forHTTPHeaderField: "Content-Length")
request.setValue("en-US", forHTTPHeaderField: "Content-Language")
Alamofire.request(request)
.validate()
.responseJSON { [weak self] response in
if response.result.isSuccess {
if let result = response.result.value {
print("Result: \(result)")
completion(result: result as! NSDictionary)
}
}
else {
print(response.debugDescription)
}
}
Và các bản ghi là
[Request]: <NSMutableURLRequest: 0x18855620> { URL: http://....... (url)}
[Response]: nil
[Data]: 0 bytes
[Result]: FAILURE: Error Domain=NSURLErrorDomain Code=-1001 "The request timed out." UserInfo={NSErrorFailingURLStringKey=http://.....(url) NSErrorFailingURLKey=http://.....(url), NSLocalizedDescription=The request timed out., _kCFStreamErrorDomainKey=4, NSUnderlyingError=0x18a08900 {Error Domain=kCFErrorDomainCFNetwork Code=-1001 "(null)" UserInfo={_kCFStreamErrorDomainKey=4, _kCFStreamErrorCodeKey=-2102}}}
[Timeline]: Timeline: { "Request Start Time": 493582123.103, "Initial Response Time": 493582138.254, "Request Completed Time": 493582138.254, "Serialization Completed Time": 493582138.256, "Latency": 15.151 secs, "Request Duration": 15.151 secs, "Serialization Duration": 0.002 secs, "Total Duration": 15.153 secs }
Tôi biết tôi có thể làm tăng thời gian timeout để tránh lỗi. Nhưng tôi muốn biết lý do thực sự tại sao nó lại ném lỗi. Không có API nào của tôi mất hơn 2 giây để trả lại dữ liệu. Sau đó, tại sao nó hiển thị độ trễ 15.151 giây.
Tôi đang sử dụng LAMP stack trên backend. Bất kỳ trợ giúp sẽ được đánh giá cao.
Bạn có thấy yêu cầu mà thời gian nhận được trên máy chủ của bạn không? Mất bao lâu để máy chủ xử lý? –
Bạn có nhấn đúng URL giống nhau không? Có vẻ như tôi không gặp vấn đề với ứng dụng của bạn. Nhiều khả năng, bạn thực sự làm bạn có một kịch bản mà API của bạn chậm. –
Bạn đã cố thực hiện yêu cầu này trên một máy khách REST chưa? Nó có hết thời gian không? – quant24