Database Reference
In-Depth Information
Figure 4-3. Connecting to a bad address on a good server
Example 4-2. A more robust success method
-(IBAction) showNews:(id) sender {
NSURL *url = [NSURL URLWithString:@"http://www.cnn.com/index.html"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[NSURLConnection
sendAsynchronousRequest:request
queue:[NSOperationQueue currentQueue]
completionHandler:^(NSURLResponse *response,
NSData *data, NSError *error) {
if (error != nil) {
[self handleError:[error localizedDescription]];
} else {
if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
NSHTTPURLResponse *httpresp =
(NSHTTPURLResponse *) response;
if ([httpresp statusCode] == 404) {
[self handleError:@"Page not found!"];
} else {
[self displayResponse:data];
}
}
}
}];
}
What has happened here is that we have checked to see that the response is really an
NSHTTPURLResponse object (which it should be for any HTTP request). If so, you can cast
the response object to that type, and use the statusCode method to check that you
haven't gotten a 404, 401, or other HTTP protocol error.
 
Search WWH ::




Custom Search