Receive HTTP Headers via NSURLRequest/NSURLConnection in Cocoa

10,466

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.

Share:
10,466
Admin
Author by

Admin

Updated on June 06, 2022

Comments

  • Admin
    Admin about 2 years

    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?

  • Julian F. Weinert
    Julian F. Weinert over 11 years
    I figured that our too. Now I extracted one header field containing more information: `Www-Authenticate: Digest realm=\"test\" qop=\"auth\"´ and so on. Is there a possibility to get / set these details as additional dict or array?