网络连接丢失 - 错误域= kCFErrorDomainCFNetwork代码= -1005 - 在GET响应中

时间:2018-06-04 10:08:29

标签: ios objective-c get afnetworking httpresponse

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

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

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

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] TI​​C阅读状态[4:0x60800016ec40]:1:57   2018-06-04 12:42:27.006807 + 0430 AppName [53990:2112128] TIC阅读状态[5:0x60c000170500]:1:57   2018-06-04 12:42:47.084334 + 0430 AppName [53990:2112128] TIC读取状态[6:0x60c00016f000]:1:57   2018-06-04 12:42:47.085662 + 0430 AppName [53990:2112128]任务< 87C4E404-57AF-47F2-A102-D516D6D9BB67>。< 1> HTTP加载失败(错误代码:-1005 [4:-4])   2018-06-04 12:42:47.085935 + 0430 AppName [53990:2112202]任务< 87C4E404-57AF-47F2-A102-D516D6D9BB67>。< 1>完成错误 - 代码:-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:[]:   错误:连接失败,错误,原因:错误域= NSURLErrorDomain代码= -1005"网络连接丢失。" UserInfo = {NSUnderlyingError = 0x60c00025c0e0 {错误域= kCFErrorDomainCFNetwork代码= -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 =网络连接丢失。}   [ERROR] 2018-06-04T12:42:47:086主题:[2110090]类:[EXExchangeClient]方法:[ - [EXExchangeClient handleAsync:andMode:] _ block_invoke @ 118] description:[]:   错误:连接失败,错误,原因:错误域= NSURLErrorDomain代码= -1005"网络连接丢失。" UserInfo = {NSUnderlyingError = 0x60c00025c0e0 {错误域= kCFErrorDomainCFNetwork代码= -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 =网络连接丢失。}   [ERROR] 2018-06-04T12:42:47:099主题:[2110090]类:[EXSession]方法:[ - [EXSession onExchangeException:] @ 159] description:[]:   错误:网络连接丢失。   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]描述:[]:   错误:网络连接丢失。   [DEBUG] 2018-06-04T12:42:47:100 thread:[2110090] class:[EXSession] method:[ - [EXSession goRequestExchangeException:] @ 346] description:[]:   交换结束   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]描述:[]:   交换结束   [ERROR] 2018-06-04T12:42:47:100主题:[2110090]类:[EXSession]方法:[ - [EXSession goRequestExchangeException:] @ 348] description:[]:   SERVER-ERROR:网络连接丢失。   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]描述:[]:   SERVER-ERROR:网络连接丢失。

我想这个问题来自Apache。

这个问题有什么解决方案吗? 谢谢你的帮助。

2 个答案:

答案 0 :(得分:1)

最后,三周后我找到了解决方案。 我们在标头中设置“ Content-length”,然后Apache(WWW)删除具有它的那些请求。所以我删除了该标头,现在一切正常:)

答案 1 :(得分:0)

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

就我而言,问题是来自服务器Sucuri / Cloudproxy的API请求被阻止(或者可以说是防火墙服务)。禁用防火墙解决了该问题

Resolution Image