2013-05-16 50 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对象

相关问题