I use NSString method initWithContentsOfURL:usedEncoding:error: to get content of some page. I notice, when page that i try to access is not exist this method is executed long time and then fail with timeout error. I tried to use NSURLRequest and NSURLConnection classes for the same purposes, but get the same result – execution long time and then timeout error.
When i try to open the same page in browser, i get response more quickly and it returns page is not available error.
It looks like cocoa methods don’t do a dns resolution for page name, or they have longer timeout for that operation.
So my question, does cocoa method that i use do dns resolve? How to do that if they didn’t?
Samples of code i use:
NSURL* url = [NSURL URLWithString:@"http://unexisting.domain.local"];
NSError* err = nil;
NSString* content = [NSString stringWithContentsOfURL:url usedEncoding:nil error:&err];
if (err) {
NSLog(@"error: %@", err);
} else {
NSLog(@"content: %@", content);
}
NSURL* url = [NSURL URLWithString:@"http://unexisting.domain.local"];
NSURLRequest* request = [NSURLRequest requestWithURL:url];
NSURLResponse* response = nil;
NSError* err = nil;
NSData* data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
if (err) {
NSLog(@"error: %@", err);
} else {
NSString* content = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"content: %@", content);
}
Thanks!
Gene M. answered how to do that with using of
SCNetworkReachability. Here is sample code: