2014-11-20 70 views
1

有谁知道如何发送如果修改标题与URL请求?iOS:发送如果修改由于标头与请求

我想要返回一个状态码,告诉我一个RSS页面是否已经更新。

更新#1: 我能够做一个测试执行以下操作:

NSURL *url = [NSURL URLWithString:self.url]; 
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; 
[request addValue:@"Tue, 18 Nov 2014 20:46:46 GMT" forHTTPHeaderField:@"If-Modified-Since"]; 
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { 
    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response; 

    int responseCode = (int)[httpResponse statusCode]; 
    NSLog(@"All headers: %@", [httpResponse allHeaderFields]); 
    NSLog(@"Status code:: %d", responseCode); 
}]; 

有什么奇怪的,虽然,如果我删除我的整个iOS的应用程序并运行它,我得到304个状态码。但是,如果我注释掉添加If-Modified-Since值的行并重新运行它,那么我会得到200,但是当我取消注释该行并重新运行它时 - 那么304错误不会回来...这是一个200错误代码。获取304错误的唯一方法是从我的iPhone设备上删除整个应用程序。

任何想法发生了什么?

+0

检查答案相同,这里 http://stackoverflow.com/questions/31551964/how-to-use-nsurlsession-to-determine-if-resource-has-changed/31567943#31567943 – mkumar 2015-07-23 10:51:45

+0

与 相同的帖子http://stackoverflow.com/questions/31551964/how-to-use-nsurlsession-to-determine-if-resource-has-changed/31567943#31567943 – mkumar 2015-07-23 10:52:53

回答

7

有点晚了,但由于NSURLConnection缓存响应,以获得304代码,你应该像这样创建你的请求。

NSURLRequest *request = [NSURLRequest requestWithURL:url 
             cachePolicy:NSURLRequestReloadIgnoringCacheData 
            timeoutInterval:60.0];