NSURLConnection & Using HEAD Method

| No Comments

If you want to use the HEAD method with NSURLConnection, it’s simple, you use -[NSMutableURLRequest setHTTPMethod:].

However, you need to be careful with redirects. Redirections will revert to using the GET method which usually isn’t what you want, so you need something like:

- (NSURLRequest *)connection:(NSURLConnection *)connection
             willSendRequest:(NSURLRequest *)request
            redirectResponse:(NSURLResponse *)redirectResponse
{
    if ([[request HTTPMethod] isEqualTo:@"HEAD"])
        return request;

    NSMutableURLRequest *newRequest = [request mutableCopy];
    [newRequest setHTTPMethod:@"HEAD"];

    return [newRequest autorelease];
}

I suspect that this bug report is invalid: http://openradar.appspot.com/7019347.

Leave a comment