views:

294

answers:

2

I want to get only the headers of an URL request. I have been using stringWithContentsOfURL() for all downloading so far, but now I am only interested in the headers and downloading the entire file is not feasible as it is too large.

I have found solutions which show how to read the headers after the response has been receieved, but how do I specify in the request that I only wish to download headers. Skip the body!

Thanks.

A: 

only implement didReceiveResponse delegate method

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    NSInteger statusCode = [(NSHTTPURLResponse*)response statusCode];
}
andi1492
+1  A: 

What I've used. The code below is synchronous but you can make it asynchronous using a delegate.

    NSURLRequest *newRequest = ... //init your request...
    NSURLResponse *response=nil;
    NSError *error=nil;

    [newRequest setValue:@"HEAD" forKey:@"HTTPMethod"];
    [NSURLConnection sendSynchronousRequest:newRequest returningResponse:&response error:&error];
    [newRequest release];
    //Get MIME type from the response
    NSString* MIMEType = [response MIMEType];
FenchKiss Dev
You have to use NSMutableURLRequest instead of NSURLRequest and then call setHTTPMethod:@"HEAD". I did not confirm whether this works. It should..
amit