【问题标题】:Async NSURLConnection sometimes not reaching server side异步 NSURLConnection 有时无法到达服务器端
【发布时间】:2013-05-11 03:16:31
【问题描述】:

我已经阅读了所有类似的问题,但没有一个答案对我有用。所以让我向你解释一下我的问题是什么: 我正在使用 NSURLConnectionasync GET 请求 发送到我的后端。我有时会得到正确的响应,但大多数时候我会从 GET 请求中获取最后获取的结果(所以我认为它必须是带有 URL 缓存的东西)。

这是我创建和发送 URL 请求的代码:

    NSMutableURLRequest *URLRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString: URL] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:kRequestTimeOut];
[URLRequest setHTTPMethod: @"GET"];

NSOperationQueue *queue = [[NSOperationQueue alloc] init];

if ([NSURLConnection canHandleRequest:URLRequest]) {
    [NSURLConnection sendAsynchronousRequest: URLRequest
                                       queue: queue
                           completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
        [queue release];

        IPBetaLog(@"RESPONSE FROM %@: %@",URL,[[[NSString alloc] initWithData: data encoding:NSUTF8StringEncoding] autorelease]);
        IPBetaLog(@"RESPONSE CODE: %i",[(NSHTTPURLResponse*) response statusCode]);

        if ([data length] > 0) {
            int responseCode = 0;
            if ([response isKindOfClass: [NSHTTPURLResponse class]]) {
                responseCode = [(NSHTTPURLResponse*) response statusCode];

                IPBetaLog(@"RESPONSE CODE: %i",responseCode);

                switch (responseCode) {
                    case 200:
                        IPLog(@"Received message 'OPERATION_COMPLETED'");
                        break;
                    case 201:
                        IPLog(@"Received message 'OPERATION_COMPLETED'");
                        break;
                    case 202:
                        IPLog(@"Received message 'OPERATION_COMPLETED'");
                        break;
                    default:
                        return block(NO,nil,[self errorWithResponseCode: responseCode]);
                        break;
                }
            }

            NSError *jsonError = nil;
            NSDictionary *result = [NSJSONSerialization JSONObjectWithData: data options: NSJSONReadingMutableContainers error:&jsonError];

            NSLog(@"Result is %@", result);
            if (jsonError != nil) {
                NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
                [userInfo setObject: NSLocalizedString(@"JSON error", @"") forKey: NSLocalizedDescriptionKey];
                [userInfo setObject: jsonError.localizedFailureReason forKey: NSLocalizedFailureReasonErrorKey];
                return block(NO,nil,[NSError errorWithDomain: MyErrorDomain code: IPRequestErrorJSONError userInfo: userInfo]);
            } else {
                return block(YES,result,nil);
            }
        }
        else if ([data length] == 0 && error == nil) {
            NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
            [userInfo setObject: NSLocalizedString(@"Response is empty", @"") forKey: NSLocalizedDescriptionKey];
            return block(NO,nil,[NSError errorWithDomain: MyErrorDomain code: IPRequestErrorEmptyReponse userInfo: userInfo]);
        }
        else if (error != nil && error.code == 1001) {
            NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
            [userInfo setObject: NSLocalizedString(@"Request timed out", @"") forKey: NSLocalizedDescriptionKey];
            return block(NO,nil,[NSError errorWithDomain: MyErrorDomain code: IPRequestErrorTimedOut userInfo: userInfo]);
        }
        else if (error != nil) {
            return block(NO,nil,error);
        }
    }];
}
else {
    NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
    [userInfo setObject: NSLocalizedString(@"No Connection", @"Error message displayed when not connected to the Internet.") forKey: NSLocalizedDescriptionKey];
    return block(NO,nil,[NSError errorWithDomain: MyErrorDomain code: IPRequestErrorBadRequest userInfo: userInfo]);
}

我正在使用 cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData,尝试了其他枚举的缓存策略,但问题仍然存在。 只是为了证明问题出在 iOS 上,我已经成功地从 Android 和 Rest Console 发送了相同的请求,并且每次都有效。

从我的后端来看,来自 iOS 的请求只是有时会出现,并且结果数据良好时才会出现这种情况。 例如: 我向服务器端发送一个带有 URL 的 GET 请求,请求来了,我做出响应,它带着响应数据到达 iOS。我向同一个 URL 发送了另一个相同的 GET 请求,但服务器端没有请求,iOS 给我与前一个请求相同的响应数据。我很少成功到达服务器端。真的不知道为什么。 iPhone 和服务器端之间没有网络问题。

如果您有任何解决方案,请。 非常感谢您抽出宝贵时间解决我的问题。

【问题讨论】:

    标签: ios caching asynchronous nsurlconnection


    【解决方案1】:

    你必须实现 NSURLConnection 委托和覆盖方法

    - (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse {
       return nil;
    }
    

    返回零。保持NSURLRequest缓存策略为NSURLRequestReloadIgnoringCacheData

    【讨论】:

    • 我已经尝试过了,但没有任何帮助。方法 POST、PUT、DELETE 工作正常,但 GET 工作不正常 - 未到达服务器。
    • 有什么解决办法吗?
    【解决方案2】:

    我通过在对服务器的每个请求的末尾添加查询参数来解决问题。我忽略了服务器端的参数。该参数是以毫秒为单位的时间戳,因此每个请求都会有所不同,并且不会涉及任何缓存。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-11
      • 2010-10-22
      • 2019-01-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多