1

Our requirements include checking Internet access to a specific file on the web-server. This file is checked every n minutes. NSURLRequests never calls connection:didFailWithError whether or not there is an internet connection. And the HTTP status is always 200. Apple's reachibility only works for domains, not files- so it doesn't meet the requirements. How can I reliably discover if I can reach this file every n minutes? Why isn't the http status code really the http status code?

Other stackoverflow questions that would seem to answer this question do not work:
1. How could connectionDidFinishLoading: run if no file is found on server?
2. Testing use of NSURLConnection with HTTP response error statuses

I tried using another queue with a completion block, but that also didn't work.

-(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");        
}
4

2 回答 2

0

感谢 Wain 和 Rob 让我走上了正确的道路。保持缓存清除的一种方法是将此方法添加到您的 NSURLConnectionDelegate:

- (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse
{
    return nil;
}
于 2013-05-21T17:27:08.443 回答
0

尝试在构造 NSURLRequest 对象时将 cachePolicy 设置为 NSURLRequestReloadIgnoringCacheData

于 2013-05-16T21:51:58.007 回答