应用启动后几秒内NSURLErrorDomain Code = -1004

时间:2021-11-24 03:53:14

I'm getting "NSURLErrorDomain Code=-1004" error with Alamofire API calls, but only for few seconds after app started (or took a rest for few minutes while the app's opened and make a call after that)

我在Alamofire API调用时出现“NSURLErrorDomain Code = -1004”错误,但仅在应用程序启动后几秒钟(或者在应用程序打开后休息几分钟并在此之后拨打电话)

If I try to make the same call after few seconds, everything works fine. I searched all the Stack Overflow questions and checked all the possible causes below:

如果我在几秒钟后尝试拨打同一个电话,一切正常。我搜索了所有Stack Overflow问题并检查了以下所有可能的原因:

  1. No problem with Internet connection
  2. 互联网连接没问题
  3. "App Transport Security Settings" are correct and server's using https (I also tried "NSAllowsArbitraryLoads=true" but that didn't help)
  4. “应用程序传输安全设置”是正确的,服务器使用https(我也试过“NSAllowsArbitraryLoads = true”,但没有帮助)
  5. APIs working fine
  6. API工作正常

My gut feeling is that getting the network settings takes few seconds and when I make an API call before that's done, it just fails immediately. OR.. I'm using a Websocket in background that could be related?

我的直觉是,获取网络设置需要几秒钟,当我在完成之前进行API调用时,它会立即失败。或者..我在背景中使用可能相关的Websocket?

FAILURE: Error Domain=NSURLErrorDomain Code=-1004 "Could not connect to the server." UserInfo={NSUnderlyingError=0x137d39380 {Error Domain=kCFErrorDomainCFNetwork Code=-1004 "(null)" UserInfo={NSErrorPeerAddressKey={length = 16, capacity = 16, bytes = 0x100201bb341a9f540000000000000000}, _kCFStreamErrorCodeKey=-2200, _kCFStreamErrorDomainKey=4}}, NSErrorFailingURLStringKey=[FILTERED], NSErrorFailingURLKey=[FILTERED], _kCFStreamErrorDomainKey=4, _kCFStreamErrorCodeKey=-2200, NSLocalizedDescription=Could not connect to the server.}

FAILURE:Error Domain = NSURLErrorDomain Code = -1004“无法连接到服务器。” UserInfo = {NSUnderlyingError = 0x137d39380 {Error Domain = kCFErrorDomainCFNetwork Code = -1004“(null)”UserInfo = {NSErrorPeerAddressKey = {length = 16,capacity = 16,bytes = 0x100201bb341a9f540000000000000000},_ kCFStreamErrorCodeKey = -2200,_kCFStreamErrorDomainKey = 4}}, NSErrorFailingURLStringKey = [FILTERED],NSErrorFailingURLKey = [FILTERED],_ kCFStreamErrorDomainKey = 4,_kCFStreamErrorCodeKey = -2200,NSLocalizedDescription =无法连接到服务器。}

Any suggestions?

有什么建议么?

UPDATED

更新

Found that app makes 4 requests on launch, and 1 or 2 of them randomly fails, and I checked Nginx access and error log and there's no log for the failed calls at all.

发现应用程序在启动时发出4个请求,其中1个或2个随机失败,我检查了Nginx访问和错误日​​志,根本没有记录失败的调用。

6 个解决方案

#1


16  

We have the same issue here with Nginx 1.10.0 (and 1.9.15), iOS 9.3.1 using HTTP/2 with TLS 1.2.

我们在Nginx 1.10.0(和1.9.15),iOS 9.3.1使用HTTP / 2和TLS 1.2时遇到了同样的问题。

The issue goes away with HTTP/1.1 and it's also working with HTTP/2 in Nginx version up to 1.9.14.

这个问题在HTTP / 1.1中消失了,它也在Nginx版本中使用HTTP / 2,最高可达1.9.14。

#2


6  

Nginx 1.11.0 Mainline is now available with the fix included mentioned earlier in this topic;

Nginx 1.11.0 Mainline现在可以使用本主题前面提到的修复程序;

Change: HTTP/2 clients can now start sending request body immediately; the "http2_body_preread_size" directive controls size of the buffer used before nginx will start reading client request body.

更改:HTTP / 2客户端现在可以立即开始发送请求正文; “http2_body_preread_size”指令控制在nginx开始读取客户端请求主体之前使用的缓冲区的大小。

I tested it and for me this release now works correctly again.

我测试了它,对我来说这个版本现在再次正常工作。

#3


3  

This seems like a confirmed bug in nginx 1.10. An issue about it can be found on the bug tracker of nginx at https://trac.nginx.org/nginx/ticket/979. The actual issue can be found at https://trac.nginx.org/nginx/ticket/959

这似乎是nginx 1.10中确认的错误。有关它的问题可以在https://trac.nginx.org/nginx/ticket/979上的nginx的bug跟踪器中找到。实际问题可以在https://trac.nginx.org/nginx/ticket/959找到

You might want to consider switching to the 1.9 branch which has releases which do work. Hopefully nginx will release a 1.10.1 version soon which does not have this bug.

您可能需要考虑切换到1.9分支,该分支具有可行的版本。希望nginx很快会发布一个没有这个bug的1.10.1版本。

The issue actually only occurs on iOS; Android, Windows and OSX itself seem to have no problems negotiating a valid http2 connection.

这个问题实际上只发生在iOS上; Android,Windows和OSX本身在协商有效的http2连接时似乎没有问题。

#4


2  

I can also confirm that the nginx 1.9.15 not works correctly. Some calls always got "Could not connect to the server", and after revert to nginx 1.9.12 everything works fine.

我还可以确认nginx 1.9.15无法正常工作。有些调用总是“无法连接到服务器”,并在恢复到nginx 1.9.12之后一切正常。

#5


1  

These are the steps that I would try to follow:

这些是我试图遵循的步骤:

  • 1) test my app in the simulator and device
  • 1)在模拟器和设备中测试我的应用程序
  • 2) look if https is really needed instead of http
  • 2)看看是否真的需要https而不是http
  • 3) configure an alamofire manager and change timeout (for this step i write some code):

    3)配置alamofire管理器并更改超时(为此步骤我编写一些代码):

    var alamofireManager = Alamofire.Manager.sharedInstance    
    let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
    configuration.HTTPMaximumConnectionsPerHost = 10
    configuration.timeoutIntervalForRequest = 30
    configuration.timeoutIntervalForResource =  30
    alamofireManager.delegate.taskWillPerformHTTPRedirection = nil
    

(so whit this last step the next alamofire calls can be for example: alamofireManager.request(etc....)

(所以在最后一步,下一个alamofire调用可以是例如:alamofireManager.request(etc ....)

  • 4) test with hard link like http://www.google.com, if the same thing dont happened nothing around your swift code are incorrect, try to set your web server parameters..
  • 4)使用像http://www.google.com这样的硬链接进行测试,如果你的swift代码周围没有发生任何相同的事情是不正确的,请尝试设置你的web服务器参数..

#6


0  

Problem solved!!!

问题解决了!!!

versions:

版本:

1. Nginx version: 1.10.2
2. IOS version: 9.3.2

When the config like this:

当配置像这样:

listen 443 ssl; 

Have the same problem like you.

和你一样有同样的问题。

But !!!

但是!!!

When the config like this:

当配置像这样:

listen 443 ssl http2;

Problem solved!!

问题解决了!!

#1


16  

We have the same issue here with Nginx 1.10.0 (and 1.9.15), iOS 9.3.1 using HTTP/2 with TLS 1.2.

我们在Nginx 1.10.0(和1.9.15),iOS 9.3.1使用HTTP / 2和TLS 1.2时遇到了同样的问题。

The issue goes away with HTTP/1.1 and it's also working with HTTP/2 in Nginx version up to 1.9.14.

这个问题在HTTP / 1.1中消失了,它也在Nginx版本中使用HTTP / 2,最高可达1.9.14。

#2


6  

Nginx 1.11.0 Mainline is now available with the fix included mentioned earlier in this topic;

Nginx 1.11.0 Mainline现在可以使用本主题前面提到的修复程序;

Change: HTTP/2 clients can now start sending request body immediately; the "http2_body_preread_size" directive controls size of the buffer used before nginx will start reading client request body.

更改:HTTP / 2客户端现在可以立即开始发送请求正文; “http2_body_preread_size”指令控制在nginx开始读取客户端请求主体之前使用的缓冲区的大小。

I tested it and for me this release now works correctly again.

我测试了它,对我来说这个版本现在再次正常工作。

#3


3  

This seems like a confirmed bug in nginx 1.10. An issue about it can be found on the bug tracker of nginx at https://trac.nginx.org/nginx/ticket/979. The actual issue can be found at https://trac.nginx.org/nginx/ticket/959

这似乎是nginx 1.10中确认的错误。有关它的问题可以在https://trac.nginx.org/nginx/ticket/979上的nginx的bug跟踪器中找到。实际问题可以在https://trac.nginx.org/nginx/ticket/959找到

You might want to consider switching to the 1.9 branch which has releases which do work. Hopefully nginx will release a 1.10.1 version soon which does not have this bug.

您可能需要考虑切换到1.9分支,该分支具有可行的版本。希望nginx很快会发布一个没有这个bug的1.10.1版本。

The issue actually only occurs on iOS; Android, Windows and OSX itself seem to have no problems negotiating a valid http2 connection.

这个问题实际上只发生在iOS上; Android,Windows和OSX本身在协商有效的http2连接时似乎没有问题。

#4


2  

I can also confirm that the nginx 1.9.15 not works correctly. Some calls always got "Could not connect to the server", and after revert to nginx 1.9.12 everything works fine.

我还可以确认nginx 1.9.15无法正常工作。有些调用总是“无法连接到服务器”,并在恢复到nginx 1.9.12之后一切正常。

#5


1  

These are the steps that I would try to follow:

这些是我试图遵循的步骤:

  • 1) test my app in the simulator and device
  • 1)在模拟器和设备中测试我的应用程序
  • 2) look if https is really needed instead of http
  • 2)看看是否真的需要https而不是http
  • 3) configure an alamofire manager and change timeout (for this step i write some code):

    3)配置alamofire管理器并更改超时(为此步骤我编写一些代码):

    var alamofireManager = Alamofire.Manager.sharedInstance    
    let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
    configuration.HTTPMaximumConnectionsPerHost = 10
    configuration.timeoutIntervalForRequest = 30
    configuration.timeoutIntervalForResource =  30
    alamofireManager.delegate.taskWillPerformHTTPRedirection = nil
    

(so whit this last step the next alamofire calls can be for example: alamofireManager.request(etc....)

(所以在最后一步,下一个alamofire调用可以是例如:alamofireManager.request(etc ....)

  • 4) test with hard link like http://www.google.com, if the same thing dont happened nothing around your swift code are incorrect, try to set your web server parameters..
  • 4)使用像http://www.google.com这样的硬链接进行测试,如果你的swift代码周围没有发生任何相同的事情是不正确的,请尝试设置你的web服务器参数..

#6


0  

Problem solved!!!

问题解决了!!!

versions:

版本:

1. Nginx version: 1.10.2
2. IOS version: 9.3.2

When the config like this:

当配置像这样:

listen 443 ssl; 

Have the same problem like you.

和你一样有同样的问题。

But !!!

但是!!!

When the config like this:

当配置像这样:

listen 443 ssl http2;

Problem solved!!

问题解决了!!