Ios 异步NSURLConnection有时无法到达服务器端

Ios 异步NSURLConnection有时无法到达服务器端,ios,caching,asynchronous,nsurlconnection,Ios,Caching,Asynchronous,Nsurlconnection,我读过所有类似的问题,但没有一个答案对我有用。那么,让我向你解释我的问题是什么: 我正在使用NSURLConnection向后端发送异步GET请求。有时我会得到正确的响应,但大多数时候我都会从get请求中得到最后一次获取的结果,所以我认为这一定是URL缓存 这是我创建和发送URL请求的代码: NSMutableURLRequest *URLRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString: URL] ca

我读过所有类似的问题,但没有一个答案对我有用。那么,让我向你解释我的问题是什么: 我正在使用NSURLConnection向后端发送异步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控制台发送了相同的请求,每次都有效

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


如果您有任何解决方案,请联系我们。非常感谢您花时间讨论我的问题。

您必须实现NSURLConnection委托和重写方法

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

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

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

我尝试过,但没有任何帮助。方法POST、PUT、DELETE工作正常,但GET工作不正常-无法到达服务器。有解决方案吗?