首页 > 解决方案 > 网络连接丢失 - 错误域 = kCFErrorDomainCFNetwork 代码 = -1005 - 在 GET 响应中

问题描述

我只是在 GET 请求中收到此错误。在服务器端,我们有管理对 Tomcat 的请求和响应的 Apache HTTP 服务器。

有两个客户端Android和iOS。Android 应用程序中的一切工作正常。另一方面,在 iOS 应用程序中,所有的 POST 响应都可以,问题只是 GET 响应。

我跟踪了请求,所有请求都在服务器端应用程序上收到,关于日志,服务器将响应发送给客户端,但客户端没有得到任何响应。

这些解决方案都不适用于我的情况。

我的一段代码:

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
manager.responseSerializer = [[AFHTTPResponseSerializer alloc] init];
NSData *data = [request.Data dataUsingEncoding:NSUTF8StringEncoding];
NSString *length = [NSString stringWithFormat:@"%lu", [data length]];
NSMutableURLRequest *urlRequest = [PagingManager makeURLRequest:length andUrl:_url];
[urlRequest setHTTPMethod:@"GET"];
NSArray *arr = [request.Data componentsSeparatedByString:@" "];
NSString *session = [PagingManager normalizeGetRequest:arr[0]];
NSString *query = [PagingManager normalizeGetRequest:arr[1]];
NSString *getReq = [NSString stringWithFormat:@"?s=%@&q=%@", session, query];
NSURL *getURL = [NSURL URLWithString:[_url concat:getReq]];
urlRequest.URL = getURL;
NSURLSessionDataTask *dataTask = [manager dataTaskWithRequest:urlRequest uploadProgress:nil downloadProgress:nil completionHandler:^
(NSURLResponse *response, id responseObject, NSError *error) {
    if (error) {
        DDLogError(@"ERROR: %@, reason: %@", @"connection failed with error", [error debugDescription]);

        NSString *const TIMEOUT = @"TimeOut";
        NSException *p = [[NSException alloc] initWithName:EXExceptionOnExchangeClientConnectionFail
                                                    reason:(error.code == NSURLErrorTimedOut) ? TIMEOUT : [error localizedDescription]
                                                  userInfo:nil];
        EXExchangeExceptionEventArgs *eventArgs = [[EXExchangeExceptionEventArgs alloc] initWithRequest:_request andError:p];
        [self exchangeException:eventArgs];

        _isRunning = NO;

    } else {
        NSString *responseString = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        NSData *decryptData = [[IOCryptographer Current] asDataDecryptString:responseString];
        if (_request.ReqMethod == GET) {
            DDLogDebug(@"Cache-Control Max-Age : %@", ((NSHTTPURLResponse *) response).allHeaderFields[@"Cache-Control"]);
            DDLogDebug(@"All header of response in GET : %@", ((NSHTTPURLResponse *) response).allHeaderFields);
            DDLogDebug(@"response: %@  and responseObject: %@", response, responseObject);

            NSCachedURLResponse *cachedURLResponse = [[NSCachedURLResponse alloc] initWithResponse:response data:decryptData];
            [NSURLCache.sharedURLCache storeCachedResponse:cachedURLResponse forRequest:cacheRequest];
        } else {
            DDLogDebug(@"All header of response in POST : %@", ((NSHTTPURLResponse *) response).allHeaderFields);
        }

        [_dataStream appendData:decryptData];
        [self connectionDidFinishLoadingWithData:responseString];
    }
}];

[dataTask resume];

和我的应用程序的日志:

原因:错误域=NSURLErrorDomain 代码=-1005 “网络连接丢失。” UserInfo={NSUnderlyingError=0x60c00025c0e0 {错误域=kCFErrorDomainCFNetwork Code=-1005 "(null)" UserInfo={_kCFStreamErrorCodeKey=-4, _kCFStreamErrorDomainKey=4}}, NSErrorFailingURLStringKey=https://{App URL}/?s={session id}, NSErrorFailingURLKey=https://{App URL}/?s={session id}, _kCFStreamErrorDomainKey=4, _kCFStreamErrorCodeKey=-4, NSLocalizedDescription=网络连接丢失。} [错误] 2018-06-04T12:42 :47:086 线程:[2110090] 类:[EXExchangeClient] 方法:[-[EXExchangeClient handleAsync:andMode:]_block_invoke@118] 描述:[]:错误:连接失败并出现错误,原因:错误域=NSURLErrorDomain 代码=- 1005 “网络连接丢失。” UserInfo={NSUnderlyingError=0x60c00025c0e0 {错误域=kCFErrorDomainCFNetwork Code=-1005 "(null)" UserInfo={_kCFStreamErrorCodeKey=-4, _kCFStreamErrorDomainKey=4}}, NSErrorFailingURLStringKey=https://{App URL}/?s={session id}, NSErrorFailingURLKey=https://{App URL}/?s={session id}, _kCFStreamErrorDomainKey=4, _kCFStreamErrorCodeKey=-4, NSLocalizedDescription=网络连接丢失。} [错误] 2018-06-04T12:42 :47:099 线程:[2110090] 类:[EXSession] 方法:[-[EXSession onExchangeException:]@159] 描述:[]:错误:网络连接丢失。2018-06-04 12:42:47.100285+0430 AppName[53990:2112202] [错误] 2018-06-04T12:42:47:099 线程:[2110090] 类:[EXSession] 方法:[-[EXSession onExchangeException: ]@159] 描述:[]:错误:网络连接丢失。[调试] 2018-06-04T12:42:47:100 线程:[2110090] 类:[EXSession] 方法:[-[EXSession goRequestExchangeException:]@346] 描述:[]:交换结束 2018-06-04 12:42:47.101545+0430 AppName[53990:2112372] [DEBUG] 2018- 06-04T12:42:47:100 线程:[2110090] 类:[EXSession] 方法:[-[EXSession goRequestExchangeException:]@346] 描述:[]:交换结束 [错误] 2018-06-04T12:42:47 :100线程:[2110090]类:[EXSession]方法:[-[EXSession goRequestExchangeException:]@348]描述:[]:SERVER-ERROR:网络连接丢失。2018-06-04 12:42:47.102801+0430 AppName[53990:2112372] [错误] 2018-06-04T12:42:47:100 线程:[2110090] 类:[EXSession] 方法:[-[EXSession goRequestExchangeException: ]@348] 描述:[]: SERVER-ERROR: 网络连接丢失。交换结束 2018-06-04 12:42:47.101545+0430 AppName[53990:2112372] [DEBUG] 2018-06-04T12:42:47:100 线程:[2110090] 类:[EXSession] 方法:[-[EXSession goRequestExchangeException:]@346] 描述:[]: 交换结束 [错误] 2018-06-04T12:42:47:100 线程:[2110090] 类:[EXSession] 方法:[-[EXSession goRequestExchangeException:]@348] 描述:[]: SERVER-ERROR: 网络连接丢失。2018-06-04 12:42:47.102801+0430 AppName[53990:2112372] [错误] 2018-06-04T12:42:47:100 线程:[2110090] 类:[EXSession] 方法:[-[EXSession goRequestExchangeException: ]@348] 描述:[]: SERVER-ERROR: 网络连接丢失。交换结束 2018-06-04 12:42:47.101545+0430 AppName[53990:2112372] [DEBUG] 2018-06-04T12:42:47:100 线程:[2110090] 类:[EXSession] 方法:[-[EXSession goRequestExchangeException:]@346] 描述:[]: 交换结束 [错误] 2018-06-04T12:42:47:100 线程:[2110090] 类:[EXSession] 方法:[-[EXSession goRequestExchangeException:]@348] 描述:[]: SERVER-ERROR: 网络连接丢失。2018-06-04 12:42:47.102801+0430 AppName[53990:2112372] [错误] 2018-06-04T12:42:47:100 线程:[2110090] 类:[EXSession] 方法:[-[EXSession goRequestExchangeException: ]@348] 描述:[]: SERVER-ERROR: 网络连接丢失。[]:交换结束 [错误] 2018-06-04T12:42:47:100 线程:[2110090] 类:[EXSession] 方法:[-[EXSession goRequestExchangeException:]@348] 描述:[]:服务器错误:网络连接丢失。2018-06-04 12:42:47.102801+0430 AppName[53990:2112372] [错误] 2018-06-04T12:42:47:100 线程:[2110090] 类:[EXSession] 方法:[-[EXSession goRequestExchangeException: ]@348] 描述:[]: SERVER-ERROR: 网络连接丢失。[]:交换结束 [错误] 2018-06-04T12:42:47:100 线程:[2110090] 类:[EXSession] 方法:[-[EXSession goRequestExchangeException:]@348] 描述:[]:服务器错误:网络连接丢失。2018-06-04 12:42:47.102801+0430 AppName[53990:2112372] [错误] 2018-06-04T12:42:47:100 线程:[2110090] 类:[EXSession] 方法:[-[EXSession goRequestExchangeException: ]@348] 描述:[]: SERVER-ERROR: 网络连接丢失。

我猜这个问题是由 Apache 引起的。

这个问题有什么解决办法吗?感谢您的帮助。

标签: iosobjective-cgetafnetworkinghttpresponse

解决方案


最后,在 3 周后,我找到了解决方案。我们将“Content-length”设置为标头,Apache(WWW) 丢弃那些拥有它的请求。所以我删除了那个标题,现在一切正常:)


推荐阅读