我有一个应用程序,可以在Xcode6-Beta1和Xcode6-Beta2与iOS7和iOS8上正常工作。但是对于Xcode6-Beta3, Beta4, Beta5,我在iOS8上面临网络问题,但在iOS7上一切都很好。我得到错误“网络连接丢失”。错误如下:

Error: ErrorDomain =NSURLErrorDomain Code=-1005 "The network connection was lost."UserInfo=0x7ba8e5b0 {NSErrorFailingURLStringKey=, _kCFStreamErrorCodeKey=57, NSErrorFailingURLKey=, NSLocalizedDescription=网络连接丢失。, _kCFStreamErrorDomainKey=1, NSUnderlyingError=0x7a6957e0 "The network connection was lost."}

我使用AFNetworking 2。X和下面的代码片段进行网络调用:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager setSecurityPolicy:policy];
manager.requestSerializer = [AFHTTPRequestSerializer serializer];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];

[manager POST:<example-url>
   parameters:<parameteres>
      success:^(AFHTTPRequestOperation *operation, id responseObject) {
          NSLog(@“Success: %@", responseObject);
      } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
          NSLog(@"Error: %@", error);
      }];

我尝试了NSURLSession,但仍然收到相同的错误。


当前回答

我们有这个确切的错误,它原来是NSURLRequest的底层HTTP实现的问题:

据我们所知,当iOS 8/9/10/11接收到一个带有keep - alive头的HTTP响应时,它会保留这个连接以供以后重用(这是应该的),但它会保留它的时间超过keep - alive头的超时参数(它似乎总是保持连接活跃30秒)。 然后,当应用程序在不到30秒后发送第二个请求时,它会尝试重新使用可能已经被服务器丢弃的连接(如果超过了真正的Keep-Alive时间)。

以下是我们目前找到的解决方案:

Increase the timeout parameter of the server above 30 seconds. It looks like iOS is always behaving as if the server will keep the connection open for 30 seconds regardless of the value provided in the Keep-Alive header. (This can be done for Apache by setting the KeepAliveTimeout option. You can simply disable the keep alive mechanism for iOS clients based on the User-Agent of your app (e.g. for Apache: BrowserMatch "iOS 8\." nokeepalive in the mod file setenvif.conf) If you don't have access to the server, you can try sending your requests with a Connection: close header: this will tell the server to drop the connection immediately and to respond without any keep alive headers. BUT at the moment, NSURLSession seems to override the Connection header when the requests are sent (we didn't test this solution extensively as we can tweak the Apache configuration)

其他回答

如果问题发生在设备上,检查流量是否通过代理(设置> Wi-Fi > (info) > HTTP代理)。我已经设置好了与Charles一起使用的设备,但忘记了代理。似乎没有Charles实际运行,就会出现这个错误。

在iOS 8模拟器上运行时,beta 5和AFNetworking 1.3也有一个问题,导致连接错误:

Domain=NSURLErrorDomain Code=-1005 "The network connection was lost."

同样的代码在iOS 7和7.1模拟器上工作得很好,我的调试代理显示失败发生在连接实际尝试之前(即没有记录请求)。

我已经追踪到NSURLConnection的失败,并向苹果报告了这个错误。见附图第5行:

.

更改为使用https允许从iOS 8模拟器连接,尽管会出现间歇性错误。

问题仍然存在于Xcode 6.01 (gm)。

我们有这个确切的错误,它原来是NSURLRequest的底层HTTP实现的问题:

据我们所知,当iOS 8/9/10/11接收到一个带有keep - alive头的HTTP响应时,它会保留这个连接以供以后重用(这是应该的),但它会保留它的时间超过keep - alive头的超时参数(它似乎总是保持连接活跃30秒)。 然后,当应用程序在不到30秒后发送第二个请求时,它会尝试重新使用可能已经被服务器丢弃的连接(如果超过了真正的Keep-Alive时间)。

以下是我们目前找到的解决方案:

Increase the timeout parameter of the server above 30 seconds. It looks like iOS is always behaving as if the server will keep the connection open for 30 seconds regardless of the value provided in the Keep-Alive header. (This can be done for Apache by setting the KeepAliveTimeout option. You can simply disable the keep alive mechanism for iOS clients based on the User-Agent of your app (e.g. for Apache: BrowserMatch "iOS 8\." nokeepalive in the mod file setenvif.conf) If you don't have access to the server, you can try sending your requests with a Connection: close header: this will tell the server to drop the connection immediately and to respond without any keep alive headers. BUT at the moment, NSURLSession seems to override the Connection header when the requests are sent (we didn't test this solution extensively as we can tweak the Apache configuration)

我在物理设备上使用iOS 12应用程序使用我公司的服务器时也遇到了同样的问题。问题是服务器硬盘已满。释放服务器中的空间解决了这个问题。

我在另一种情况下发现了同样的错误,我认为由于超时无法通过苹果提供的标准网络API (URLSession)进行参数化。timeoutIntervalForRequest和URLSession.timeoutIntervalForResource)。即使. .使服务器应答更快解决了问题

对于我来说,重置模拟器的内容和设置是有效的。 要重置模拟器,请遵循以下步骤:

iOS Simulator -> Reset Content and Settings ->按“Reset”(在iOS模拟器上) 即将到来的警告)