网络连接丢失 - Error Domain=kCFErrorDomainCFNetwork Code=-1005 - 在 GET 响应中

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

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

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

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

None 的 THIS 解决方案适用于我的案例。

我的一段代码:

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

以及我的应用程序的日志:

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.

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

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

终于,在 3 周后我找到了解决方案。 我们将 "Content-length" 设置为 header 并且 Apache(WWW) 丢弃那些具有它的请求。所以我删除了 header 现在一切正常:)

我遇到了同样的问题,我附上了解决方案的屏幕截图,以显示我是如何解决这个问题的。

在我的例子中,问题是 API 请求被服务器 Sucuri/Cloudproxy 阻止(或者你可以说是防火墙服务)。禁用防火墙解决了问题