我的代碼的目的是比較服務器文件和本地文件的修改日期,以防服務器文件更新,它會下載它。HEADER請求allHeaderFields不工作
我第一次嘗試是使用使用的代碼同步請求從http://iphoneincubator.com/blog/server-communication/how-to-download-a-file-only-if-it-has-been-updated
但它並沒有奏效。 之後,我一直在努力尋找解決方案,嘗試異步請求,嘗試了不同的代碼,我發現圍繞stackoverflow,谷歌等,但沒有任何作品。
如果在終端我做curl -I <url-to-file>
我得到的標題值,所以我知道不是服務器問題。
這是我與掙扎,現在(這是寫在Appdelegate.m)
- (void)downloadFileIfUpdated {
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL: url
cachePolicy: NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval: 10];
[request setHTTPMethod:@"HEAD"];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
if(!connection) {
NSLog(@"connection failed");
} else {
NSLog(@"connection succeeded");
}
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[self downloadFileIfUpdated]
}
#pragma mark NSURLConnection delegate methods
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
NSString *lastModifiedString = nil;
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
if ([response respondsToSelector:@selector(allHeaderFields)]) {
lastModifiedString = [[response allHeaderFields] objectForKey:@"Last-Modified"];
}
[Here is where the formatting-date-code and downloading would take place]
}
眼下的代碼,因爲它是,它給我的錯誤No visible @interface for 'NSURLResponse' declares de selector 'allHeaderFields'
。
當我使用同步方法時,錯誤是NSLog(@"%@",lastModifiedString)
返回(null)。注:如果有更好的方法可以解釋我自己或代碼,請告訴我。
UPDATE
我使用的URL是ftp://
類型以及可能的,爲什麼我沒有得到任何標題的問題。但我無法弄清楚如何去做。
我發現,這個問題是,我使用的是URL類型的 'FTP:// ...',這就是爲什麼我沒有收到任何標題。 但是,我不知道如何得到一個FTP網址的頭文件。 – thewinger
FTP URL不使用HTTP,所以不會有HTTP標頭(FTP本身也沒有標頭)。您*可能*能夠獲得包裝FTP響應的TCP頭部,但我不確定這是否是您要查找的內容。 –
但是,那麼,爲什麼如果我在終端中執行'curl -I ftp:// ... /',如果給我帶最後修改值的標題等? – thewinger