当我使用NSCachedURLResponse时,我得到了"301PermMove"

hmx*_*hhh 5 nsurlcache ios nscache

当我测试时NSURLCache,我得到了"301PermMove",这是我的代码

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{

NSURL *url = [NSURL URLWithString:@"https://www.github.com"];
NSURLRequest *urlRequest = [[NSURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:10];
NSURLCache *shareCache = [NSURLCache sharedURLCache];
NSCachedURLResponse *resp = [shareCache cachedResponseForRequest:urlRequest];
NSLog(@"cache data:%@",[[NSString alloc]initWithData:resp.data encoding:NSUTF8StringEncoding]);

[NSURLConnection sendAsynchronousRequest:urlRequest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {

    NSLog(@"data:%@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]);
}];
}
Run Code Online (Sandbox Code Playgroud)

第一次触摸:缓存数据为零,第二次触摸:缓存数据为301PermMove.为什么?我不知道.

小智 2

您收到 HTTP 响应状态代码 301 永久移动。这意味着您正在查找的资源已移至另一个位置。对我来说非常简单,因为问题是旧的 URL 从 http 更改为 https。不幸的是,我没有您的问题的答案,但您可以尝试访问不重定向的 URL。

希望这能有所帮助。