我有一个应用程序,可以在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,但仍然收到相同的错误。


当前回答

我通过VPN连接。去使能VPN解决了问题。

其他回答

我的问题在服务器上。我正在使用Python的BaseHTTPRequestHandler类,我没有在响应中发送正文。当我像下面这样放一个空身体时,我的问题就解决了。

def do_POST(self):
    content_len = int(self.headers.get('Content-Length'))
    post_body = self.rfile.read(content_len)
    msg_string = post_body.decode("utf-8")
    msg_json = json.loads(msg_string)
    self.send_response(200)
    self.end_headers() #this and the following lines were missing
    self.wfile.write(b'') 

我必须退出XCode,删除DerivedData文件夹内容(~/Library/Developer/ XCode /DerivedData或/Library/Developer/ XCode /DerivedData)并退出模拟器才能使其工作。

我们有这个确切的错误,它原来是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)

我也有同样的问题。我不知道AFNetworking如何实现https请求,但对我来说,原因是NSURLSession的缓存问题。

在我的应用程序从safari跟踪回来后,然后发布一个http请求,“http加载失败1005”错误将出现。 如果我停止使用“[NSURLSession sharedSession]”,而是使用一个可配置的NSURLSession实例调用“dataTaskWithRequest:”方法如下,问题就解决了。

NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
config.requestCachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
config.URLCache = nil;
self.session = [NSURLSession sessionWithConfiguration:config];

只要记住设置配置。URLCache = nil;。

在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)。