我需要在不使用缓存的情况下检查我的服务器上是否存在文件。我使用的方法都是返回200,即使文件不存在,所以我只能假设是缓存问题,或者我的代码有问题。
这是我的代码:出于参数考虑..此示例中的 URL 已更改,但我的代码中的 url 是正确的。
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,即使我重命名服务器上的文件。