代码之家  ›  专栏  ›  技术社区  ›  WrightsCS

iPhone SDK远程文件存在,没有缓存

  •  1
  • WrightsCS  · 技术社区  · 14 年前

    我需要检查我的服务器上是否存在不使用缓存的文件。我使用的方法都返回200,即使文件不存在,所以我只能假设缓存有问题,或者我的代码有问题。

    NSString *auth = [NSString stringWithFormat:@"http://www.mywebsite.com/%@.txt",[self aString]];
    NSURL *authURL = [NSURL URLWithString:auth];
    
    NSURLRequest* request = [NSURLRequest requestWithURL:authURL 
                                             cachePolicy:NSURLRequestReloadIgnoringCacheData
                                         timeoutInterval:5.0];
    NSURLConnection *conn = [NSURLConnection connectionWithRequest:request
                                                          delegate:self];
    
    NSHTTPURLResponse* response = nil;
    NSError* error = nil;
    [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    NSLog(@"statusCode = %d", [response statusCode]);
    
    if ([response statusCode] == 404)
        NSLog(@"MISSING");
    else
        NSLog(@"EXISTS");
    

    响应总是200,即使我重命名了服务器上的文件。

    2 回复  |  直到 14 年前
        1
  •  2
  •   Jason Jenkins    14 年前

    你的代码有几个潜在的问题。首先,当你创建 conn connectionWithRequest:delegate: 您正在启动一个异步请求。响应将以代表的方式接收( self 在你的情况下) connection:didReceiveResponse: sendSynchronousRequest:returningResponse:error: 是给你的。如果这是您想要的,那么您不需要前面的调用来创建连接。

    sendSynchrono公司美国请求:returningResponse:错误:

    NSData * result = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    
    if (result != nil) {
       NSLog(@"statusCode = %d", [response statusCode]);
    
       if ([response statusCode] == 404)
        NSLog(@"MISSING");
       else
           NSLog(@"EXISTS");
    } else {
      NSLog(@"%@", error);
    }
    
        2
  •  1
  •   amrox    14 年前

    有没有可能它在服务器端缓存?如果是这样,你可以试试 NSURLRequestReloadIgnoringLocalAndRemoteCacheData NSURLRequestReloadIgnoringCacheData .