Stephen W
Stephen W

Reputation: 1

URL reachablility - false positive

There are a ton of "false positive" posts, though none appear to match my situation. (Most are talking about internet connection errors..)

As with everyone almost else, I just want to determine whether a specific URL is valid. Now, I'm dealing with subdomains, and it works great as long as I choose a valid domain. If I choose an invalid subdomain, I get a false positive. Using a browser to test reveals that my ISP gives me a really annoying suggestions page if I type in a URL that doesn't exist.

Any way to validate that the responding server IS actually at the URL I asked for?

My Code:

-(BOOL) canConnectToURL:(NSString * )sURL {
    BOOL bSuccess = NO;
    if (![txtSubDomain.text.lowercaseString  isEqual: @"www"]){
        NSLog(@"canConnectToURL(%@)",sURL);
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL: [NSURL URLWithString: sURL]];

        [request setHTTPMethod: @"HEAD"];
        NSURLResponse *response;
        NSError *error;
        [NSURLConnection sendSynchronousRequest: request returningResponse: &response error: &error];
        NSLog(@"URL: %@",sURL);
        NSLog(@"data: %@",response);
        bSuccess = ([(NSHTTPURLResponse *)response statusCode] == 200);
    }
    return bSuccess;
}

Upvotes: 0

Views: 32

Answers (1)

RobP
RobP

Reputation: 9522

I believe you can attach a delegate to your NSURLRequest that handles the method connection:willSendRequest:redirectResponse: in the NSURLConnectionDataDelegate protocol, and catch it there.

https://developer.apple.com/library/mac/documentation/Foundation/Reference/NSURLConnectionDataDelegate_protocol/Reference/Reference.html#//apple_ref/doc/uid/TP40011348-CH1-SW9

Upvotes: 1

Related Questions