Search code examples
iosobjective-cgetafnetworkinghttpresponse

The network connection was lost - Error Domain=kCFErrorDomainCFNetwork Code=-1005 - in GET response


I get this error just in GET request. At the server side, we have Apache HTTP Server that manages requests and responses to Tomcat.

There are two clients Android and iOS. Everything in Android app works fine. On the other side, in iOS app all of the POST responses are OK, the problem is just with GET responses.

I tracked the requests, all of them are received on the server side app and regarding the logs, the server sends the response to the client but the client didn't get any response.

None of THIS solutions worked for my case.

A chunk of my code:

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];

And the log of my App:

2018-06-04 12:41:22.871344+0430 AppName[53990:2111182] TIC Read Status [4:0x60800016ec40]: 1:57 2018-06-04 12:42:27.006807+0430 AppName[53990:2112128] TIC Read Status [5:0x60c000170500]: 1:57 2018-06-04 12:42:47.084334+0430 AppName[53990:2112128] TIC Read Status [6:0x60c00016f000]: 1:57 2018-06-04 12:42:47.085662+0430 AppName[53990:2112128] Task <87C4E404-57AF-47F2-A102-D516D6D9BB67>.<1> HTTP load failed (error code: -1005 [4:-4]) 2018-06-04 12:42:47.085935+0430 AppName[53990:2112202] Task <87C4E404-57AF-47F2-A102-D516D6D9BB67>.<1> finished with error - code: -1005 2018-06-04 12:42:47.087246+0430 AppName[53990:2112202] [ERROR] 2018-06-04T12:42:47:086 thread:[2110090] class:[EXExchangeClient] method:[-[EXExchangeClient handleAsync:andMode:]_block_invoke@118] description:[]: ERROR: connection failed with error, reason: Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost." UserInfo={NSUnderlyingError=0x60c00025c0e0 {Error Domain=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=The network connection was lost.} [ERROR] 2018-06-04T12:42:47:086 thread:[2110090] class:[EXExchangeClient] method:[-[EXExchangeClient handleAsync:andMode:]_block_invoke@118] description:[]: ERROR: connection failed with error, reason: Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost." UserInfo={NSUnderlyingError=0x60c00025c0e0 {Error Domain=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=The network connection was lost.} [ERROR] 2018-06-04T12:42:47:099 thread:[2110090] class:[EXSession] method:[-[EXSession onExchangeException:]@159] description:[]: ERROR: The network connection was lost. 2018-06-04 12:42:47.100285+0430 AppName[53990:2112202] [ERROR] 2018-06-04T12:42:47:099 thread:[2110090] class:[EXSession] method:[-[EXSession onExchangeException:]@159] description:[]: ERROR: The network connection was lost. [DEBUG] 2018-06-04T12:42:47:100 thread:[2110090] class:[EXSession] method:[-[EXSession goRequestExchangeException:]@346] description:[]: Exchange Ended 2018-06-04 12:42:47.101545+0430 AppName[53990:2112372] [DEBUG] 2018-06-04T12:42:47:100 thread:[2110090] class:[EXSession] method:[-[EXSession goRequestExchangeException:]@346] description:[]: Exchange Ended [ERROR] 2018-06-04T12:42:47:100 thread:[2110090] class:[EXSession] method:[-[EXSession goRequestExchangeException:]@348] description:[]: SERVER-ERROR:The network connection was lost. 2018-06-04 12:42:47.102801+0430 AppName[53990:2112372] [ERROR] 2018-06-04T12:42:47:100 thread:[2110090] class:[EXSession] method:[-[EXSession goRequestExchangeException:]@348] description:[]: SERVER-ERROR:The network connection was lost.

I guess this issue arises from Apache.

Is there any solution for this issue? Thanks for helping.


Solution

  • Finally, after 3 weeks I found a solution. we set "Content-length" into header and Apache(WWW) drop those request that has it. So I removed that header and everything works fine now :)