5

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.

amit
  • 1,373
  • 2
  • 16
  • 29

3 Answers3

11

Asynchronously send a HEAD request to the URL in question and then just access the allHeaderFields property on HTTPURLResponse / NSHTTPURLResponse.

Swift 4

var request = URLRequest(url: URL(string: "https://google.com/")!)
request.httpMethod = "HEAD"
let task = URLSession.shared.dataTask(with: request) { data, response, error in
    guard let response = response as? HTTPURLResponse,
        let headers = response.allHeaderFields as? [String: String] else {
        return
    }
}
task.resume()

Objective-C

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://google.com"]];
[request setHTTPMethod:@"HEAD"];
[NSURLConnection sendAsynchronousRequest:request
                                   queue:[NSOperationQueue mainQueue]
                       completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
                           NSDictionary *headers = [(NSHTTPURLResponse *)response allHeaderFields];
                       }];
Dan Loewenherz
  • 10,879
  • 7
  • 50
  • 81
  • 1
    Preferred over @FKDev's answer, since this one suggest using the asynchronous API - and does use `NSMutableURLRequest` correctly. ;) – CouchDeveloper Jun 29 '13 at 09:31
2

only implement didReceiveResponse delegate method

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    NSInteger statusCode = [(NSHTTPURLResponse*)response statusCode];
}
Shaggy Frog
  • 27,575
  • 16
  • 91
  • 128
andi1492
  • 56
  • 2
2

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

    NSMutableURLRequest *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];

Edited to add replace NSURLRequest with NSMutableRequest.

FKDev
  • 2,266
  • 1
  • 20
  • 23
  • 3
    You have to use NSMutableURLRequest instead of NSURLRequest and then call setHTTPMethod:@"HEAD". I did not confirm whether this works. It should.. – amit Apr 05 '10 at 23:13
  • Not to nitpick the nitpicker. But, replacing "..." with "[[NSMutableURLRequest alloc] initWithURL:..." in the above answer works. So, technically, the answer is not wrong. Bad faith always wins ;) As for Synchronous/asynchronous debate, it is off topic. Dan is right though. – FKDev Feb 09 '15 at 20:02
  • Hm, you're right... I'll take back my downvote, however I need the answer to be edited to do that it seems. =) – jake_hetfield Feb 13 '15 at 11:45