5

当我测试时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]);
}];
}

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

4

1 回答 1

2

您将收到 HTTP 响应状态代码 301 Moved Permanently。这意味着您要查找的资源已移动到另一个位置。对我来说很容易,因为问题是旧 URL 从 http 更改为 https。不幸的是,我没有您的问题的答案,但您可以尝试在不重定向的情况下访问 URL。

希望这能有所帮助。

于 2020-04-01T21:23:36.913 回答