2013-05-16 96 views
1

我們的要求包括檢查對Web服務器上特定文件的Internet訪問。每隔 n分鐘檢查此文件。 NSURLRequests從不調用連接:didFailWithError是否存在互聯網連接。而HTTP狀態始終爲200.蘋果的可觸及性僅適用於域,而不適用於文件,因此它不符合要求。如何可靠地發現我是否可以每隔 n分鐘?爲什麼http狀態碼不是真正的http狀態碼?iOS NSURLRequests狀態代碼始終爲200

這似乎回答這個問題,其他的問題計算器不工作:
1. How could connectionDidFinishLoading: run if no file is found on server?
2. Testing use of NSURLConnection with HTTP response error statuses

我試着用另一隊列完成塊,但也沒有工作。

-(void) updateConnectionStatus 
{ 
    NSURL *url = [NSURL URLWithString:(NSString*)[appValues getValueForSettingsKey:@"company.project.test.pingURL"]]; 
    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url]; 
    //NSOperationQueue *queue = [[NSOperationQueue alloc] init]; 
    //__block __typeof__(self) _self = self; 
    connection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self]; 
/* 
    [NSURLConnection 
    sendAsynchronousRequest:urlRequest queue:queue 
     completionHandler:^(NSURLResponse *response, 
         NSData *data, 
         NSError *error) { 

     NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response; 
     int code = [httpResponse statusCode]; // ALWAYS 200 no matter what 
     NSString *pingFile = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 
     NSLog(@"%@",error); // NEVER has an error 
     //This doesn't even work because it remembers FOREVER the value once it gets it. 
     if ([@"Ping!" isEqualToString:pingFile]) 
     { 
      dispatch_async(dispatch_get_main_queue(), ^{      
       [_self companyConnection:YES];      
      });     
     } else { 
      dispatch_async(dispatch_get_main_queue(), ^{      
       [_self companyConnection:NO];      
      }); 
     }   
    }]; 
     */ 
} 

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
{ 
    NSLog(@"ERROR: %@", error); // Never get here 
} 

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{ 
    NSHTTPURLResponse *aResponse = (NSHTTPURLResponse*)response; 
    NSLog(@"received a response: %ld",(long)[aResponse statusCode]); 
    if ([response respondsToSelector:@selector(statusCode)]) 
    { 
     int statusCode = [((NSHTTPURLResponse *)response) statusCode]; 
     // statusCode is always 200 
     if (statusCode >= 400) 
     { 
      [companyConnection cancel]; // stop connecting; no more delegate messages 
      NSDictionary *errorInfo 
      = [NSDictionary dictionaryWithObject:[NSString stringWithFormat: 
                NSLocalizedString(@"Server returned status code %d",@""), 
                statusCode] 
              forKey:NSLocalizedDescriptionKey]; 
     } 
    } 
} 

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 
    NSLog(@"received data"); 
} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
    NSLog(@"Finished");   
} 
+0

您是否在做任何事情以防止緩存您收到的第一個響應? – Wain

+0

我將如何防止緩存第一個響應?因爲每種方法的範圍之外都沒有變量,所以在下一個請求發佈之前,這些變量似乎已經消失了。順便說一句,我正在使用ARC。 –

+0

假設你運行在最新版本的iOS上,NSURLCache將自動緩存響應(取決於返回的頭文件)。 – Wain

回答

0

由於北斗星和Rob爲把我在正確的道路。清除緩存的一種方法是將此方法添加到NSURLConnectionDelegate中:

- (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse 
{ 
    return nil; 
} 
0

嘗試用設置的CachePolicy爲NSURLRequestReloadIgnoringCacheData而構建的NSURLRequest對象

相關問題