代码= -1001“请求超时。” [英] Code=-1001 "The request timed out."

查看:3343
本文介绍了代码= -1001“请求超时。”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个需要大量使用API​​的Swift项目。一切正常,但有时(20个中有1个),我得到 Code = -1001请求超时。调用API时出错。

I am working on a Swift project which requires a lot of consumption of APIs. Everything is working fine but sometimes (1 in 20), I get Code=-1001 "The request timed out." error while calling the API.

我正在使用Alamofire。我附加代码来调用API。

I am using Alamofire. I am attaching the code to call 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)
       }
}

并且日志是

[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 }

我知道我可以增加超时时间以避免错误。但我想知道它抛出错误的实际原因。我的API都没有超过2秒的时间来返回数据。那么为什么它显示15.151秒的延迟。

I know I can increase the timeout period to avoid the error. But I want to know the actual reason why it is throwing the error. None of my API takes more than 2 seconds to return data. Then why it is showing latency of 15.151 seconds.

我在后端使用LAMP堆栈。任何帮助将不胜感激。

I am using LAMP stack on backend. Any help would be appreciated.

推荐答案

我收到Code = -1001错误请求超时。。
我试了几件事。没有任何效果。最后我发现问题出在我的参数中,我在请求体中发送到服务器的格式错误(值很好,但类型错误)。这就是为什么请求超时,因为服务器无法处理它。你可以检查一下,如果没有别的办法适用于你。

I had got the error with Code=-1001 "The request timed out.". I tried a few things. Nothing worked. Finally I found out that the problem was in my parameters that I'm sending in the request body to the server which were of wrong format(value was fine but type was wrong). Thats why the request was timing out coz the server wasn't able to process it. You can check that up if nothing else works for u.

这篇关于代码= -1001“请求超时。”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆