views:

2533

answers:

1

I've been working on figuring out how to receive HTTP Headers via a request made with NSURLConnection. Typically a request is made with something as simple as the following:

    NSURLConnection *connection = [[NSURLConnection alloc]
    initWithRequest:request
        delegate:self];

The only way I've personally found in Apple's abundant documentation to receive response headers is via a synchronous call using the following NSURLConnection class method:

+ (NSData *)sendSynchronousRequest:(NSURLRequest *)request returningResponse:(NSURLResponse **)response error:(NSError **)error

Here I can easily reference a response object of type NSURLResponse. The problem here is that I'm not ready to make synchronous requests via a mobile device, especially with a network that has high latency such as EDGE. Is it possible to get similar results with the default, asynchronous behavior of NSURLConnection?

+13  A: 

In your connection delegate, add the -connection:didReceiveResponse: method. If you're doing a standard HTTP request, the NSURLResponse object passed in will actually be an NSHTTPURLResponse object, and responds to the -allHeaderFields message. This should be what you're looking for.

Ben Gottlieb