32

How can I read the data from the header sent by in the server response. I am using NSURLConnection to send the request.

Abhinav
  • 37,684
  • 43
  • 191
  • 309

2 Answers2

78

If the URL is an HTTP URL, then the NSURLResponse that you receive in your connection's delegate's -connection:didReceiveResponse: method (or via another method) will be an NSHTTPURLResponse, which has an -allHeaderFields method that lets you access the headers.

NSURLResponse* response = // the response, from somewhere
NSDictionary* headers = [(NSHTTPURLResponse *)response allHeaderFields];
// now query `headers` for the header you want
John Calsbeek
  • 35,947
  • 7
  • 94
  • 101
4

In my case

    NSHTTPURLResponse *response = ((NSHTTPURLResponse *)[task response]);
    NSDictionary *headers = [response allHeaderFields];

Good Approach

    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)[task response];
    if ([httpResponse respondsToSelector:@selector(allHeaderFields)]) {
         NSDictionary *dictionary = [httpResponse allHeaderFields];
         NSLog(@"%@", [dictionary description]);
    }
Ali
  • 514
  • 5
  • 16