我有一个应用程序,可以在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)
我们有这个确切的错误,它原来是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)