2012-01-17 28 views
4

我使用if-modified-since在HTTP標題中決定是否應該下載文件。 應用程序已經過測試,一切正常,但現在當我詢問我的NSHTTPURLResponse實例response.statusCode[[response allHeaderFields] objectForKey:@"Last-Modified"]時,出現錯誤。NSHTTPURLResponse變爲NSURLResponse

它似乎只是NSURLResponse。可能的原因是什麼?我已閱讀this topic但問題仍然不清楚。 在此先感謝! UPD: 一些代碼:

 NSURL *url = [NSURL URLWithString:urlString]; 
     NSFileManager *fileManager = [NSFileManager defaultManager]; 

     NSMutableURLRequest *myRequest = [NSMutableURLRequest requestWithURL:url]; 
     [myRequest setHTTPMethod:@"GET"]; 

     NSDateFormatter *df = [[NSDateFormatter alloc] init]; 
     df.dateFormat = @"EEE',' dd MMM yyyy HH':'mm':'ss 'GMT'"; 
     df.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]; 
     df.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"]; 

     [myRequest addValue:[df stringFromDate:lastModifiedLocal] forHTTPHeaderField:@"If-Modified-Since"]; 

     myRequest.cachePolicy = NSURLRequestReloadIgnoringLocalAndRemoteCacheData; 

     NSHTTPURLResponse *response=nil; 
     NSError* error = nil; 

     NSData* data = [NSURLConnection sendSynchronousRequest:request    returningResponse:&response error:&error]; 

     if (error) { 
     NSLog(@"Error sending request: %@", [error localizedDescription]); 
    } 
//As was advised 
     NSHTTPURLResponse* newResp = (NSHTTPURLResponse*)response; 
//crash here   
NSLog(@"%d", newResp.statusCode); 

UPD: 錯誤代碼 - myRequest和請求是不同的變量。問題解決了。

+0

「sendSynchronousRequest:returningResponse:error:'後的'data'的結果是什麼?另外,爲什麼不嘗試傳遞實際的'NSError'指針,所以如果出現錯誤,你可能會知道它是什麼,通過記錄'error.localizedDescription'? – NJones 2012-01-17 13:58:01

+0

正如我所料,數據是正常的。 – 2012-01-18 07:39:26

+0

有一個錯誤,因爲我的愚蠢uncentration - '[myRequest addValue:[df stringFromDate:lastModifiedLocal] forHTTPHeaderField:@「If-Modified-Since」]; myRequest.cachePolicy = NSURLRequestReloadIgnoringLocalAndRemoteCacheData;'和 'NSData的數據* = [NSURLConnection的sendSynchronousRequest:請求returningResponse:響應誤差:&錯誤];'後,與本地URL的另一變量,所以沒有HTTP那裏。抱歉。將批准最完整的答案。 – 2012-01-19 09:22:35

回答

3

這很可能是請求失敗並且沒有生成響應對象。您可以檢查此如下:

if (response) { 
    NSHTTPURLResponse* newResp = (NSHTTPURLResponse*)response; 
    NSLog(@"%d", newResp.statusCode); 
} 
else { 
    NSLog(@"No response received"); 
} 

由於被另一個評論者建議,你應該有一個NSError對象,因此可以更有效地檢查錯誤:

NSHTTPURLResponse *response=nil; 
NSError *error = nil; 
NSData* data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error: &error]; 

然後,您可以檢查錯誤在檢查響應之前:

if (error) { 
    NSLog(@"Error sending request: %@", [error localizedDescription]); 
} 
+0

我做了錯誤日誌記錄,錯誤是0x0,所以沒有任何信息。代碼更新,謝謝。 – 2012-01-18 07:43:49

+0

也返回數據是OK的,只是HTTP頭在壞了,我猜。服務器端的測試表明,一切都很好。 – 2012-01-18 08:59:58

0

你可以簡單地將它轉換:

- (void)connection:(NSURLConnection *)conn didReceiveResponse:(NSURLResponse *)aResponse 
{ 
    self.response = (NSHTTPURLResponse *)aResponse; 
} 

編輯

您的回覆爲空。試試這個:

NSHTTPURLResponse *response = nil; 
NSData* data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil]; 
+0

不幸的是,它並沒有幫助。 – 2012-01-17 10:44:29

+0

如果請求是NSHTTPURLResponse類,它將工作。 – hwaxxer 2012-01-17 10:46:56

+0

您如何以及何時設置響應?什麼類型的響應? – hwaxxer 2012-01-17 10:56:36

相關問題