0

我使用此处发布的代码:
connectionDidFinishLoading 中的连接释放方法,导致错误

现在首先执行返回 didFail 日志。第二次执行;返回旧的响应数据。尽管我的(本地主机)服务器完全脱机。并且 cachePolicy 是 NSURLCacheStorageNotAllowed (检查我上面发布的链接上的代码)

 NSMutableURLRequest *request=
[NSMutableURLRequest requestWithURL:url 
cachePolicy:NSURLCacheStorageNotAllowed timeoutInterval:3.0f];

响应数据似乎缓存在某处并且仍然存在。

但是如果我使用 NSURLRequestReloadIgnoringLocalAndRemoteCacheData //注释为-未实现-

不返回旧缓存。

但如果是这样,有什么区别:
NSURLRequestReloadIgnoringLocalAndRemoteCacheData

NSURLCacheStorageNotAllowed

我该怎么办 ?

4

2 回答 2

2

NSURLCacheStorageNotAllowedNSCachedURLResponse代并且是 enum 的一个值NSURLCacheStoragePolicy。由于缓存策略NSMutableURLRequest也是一个 enum( NSURLRequestCachePolicy),因此您只需将错误的 int 传递给静态方法创建NSMutableURLRequest。在这种情况下NSURLCacheStorageNotAllowed,只有 2 等于NSURLRequestReturnCacheDataElseLoad- 这就是你得到旧数据的原因。

于 2014-07-16T14:47:05.423 回答
0

尝试这个

NSString *Post = [[NSString alloc] initWithFormat:@"Post 参数"]; NSURL *Url = [NSURL URLWithString:@"Url"];

    NSData *PostData = [Post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

    NSString *postLength = [NSString stringWithFormat:@"%d", [PostData length]];

    NSMutableURLRequest *Request = [[NSMutableURLRequest alloc] init];
    [Request setURL:Url];
    [Request setHTTPMethod:@"POST"];
    [Request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [Request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [Request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [Request setHTTPBody:PostData];
  NSError *error;
    NSURLResponse *response;

    NSData *Result = [NSURLConnection sendSynchronousRequest:Request returningResponse:&response error:&error];
    if (!Result)
    {
        NSLog(@"Error");
    }
    else
    {
       //Parse the result
    }
于 2013-09-02T08:03:32.813 回答