I need to check whether a URL (represented by a NSURL) is available or returns 404. What is the best way to achieve that?
I would prefer a way to check this without
You can achieve a synchronous connection by calling:
NSURLRequest* request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
NSHTTPURLResponse* response = nil;
NSError* error = nil;
[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
Your thread will block until the request has beeen made.
I needed a solution that didn't use a delegate either, so I took pieces of code shown in other answers here and created a simple method that works well in my case (and might be what you are looking for as well):
-(BOOL) webFileExists {
NSString *url = @"http://www.apple.com/somefile.html";
NSURLRequest* request = [NSURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:5.0];
NSHTTPURLResponse* response = nil;
NSError* error = nil;
[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSLog(@"statusCode = %d", [response statusCode]);
if ([response statusCode] == 404)
return NO;
else
return YES;
}
I used the answer from woodmantech above, but changed it based on what I have seen on other similar questions here so that it does not download the whole file to see if it exists.
I changed NSURLRequest
to NSMutableURLRequest
, and added:
[request setHTTPMethod:@"HEAD"];
This seems to work fine. I am working on my first app, so no real experience yet. Thanks everyone.
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:5.0];
[request setHTTPMethod:@"HEAD"];
NSHTTPURLResponse* response = nil;
NSError* error = nil;
[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSLog(@"statusCode = %d", [response statusCode]);
As you may know already that general error can capture by didFailWithError method:
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"Connection failed! Error - %@ %@",
[error localizedDescription],
[[error userInfo] objectForKey:NSErrorFailingURLStringKey]);
}
but for 404 "Not Found" or 500 "Internal Server Error" should able to capture inside didReceiveResponse method:
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
if ([response respondsToSelector:@selector(statusCode)])
{
int statusCode = [((NSHTTPURLResponse *)response) statusCode];
if (statusCode == 404)
{
[connection cancel]; // stop connecting; no more delegate messages
NSLog(@"didReceiveResponse statusCode with %i", statusCode);
}
}
}