Objective-C checking if URL exists in Command Line application
I have a simple command line application to test the presence of files. The files are very large, however, and I would like to chec开发者_开发知识库k that they are there without downloading the whole file. This is what I have but it downloads each file:
NSURL *url = [NSURL URLWithString:downloadPath];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLResponse *response = nil;
NSError **error=nil;
NSData *data=[[NSData alloc] initWithData:[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:error]];
NSInteger httpStatus = [((NSHTTPURLResponse *)response) statusCode];
if(httpStatus == 404)
{
NSLog(@"%@ NOT FOUND!",[productDic objectForKey:@"fileName"]);
}
The files I am looking for are .zip and are not found locally.
If you make a HEAD request instead of a GET request, you'll probably get what you're after:
NSMutableURLRequest *request = [[NSURLRequest requestWithURL:url] mutableCopy];
[request setHTTPMethod:@"HEAD"];
[request autorelease];
精彩评论