Flag This Hub

Apple's iPhone: Accessing HTTP Headers from an NSURLRequest

By


See all 3 photos

Programming the iPhone from Apple

Using NSURLRequest to send the HTTP Headers that Accompanied a Query Response

One of the more quirky programming (coding) tasks with the Apple iPhone relates to headers for http connections, and although not difficult, can be quite frustrating if the syntax isn't correct.

In the NSHTTPURLResponse class, the headers for an HTTP connection are included. By sending the allHeaderFields message with the NSHTTPURLResponse variable, you can easily get the headers out as NSDictionary.

Although synchronous requests are problematic due to blocking issues, populating an NSHTTPURLResponse in this manner is fairly straightforward:

Click on the image of code to increase the size and more clearly read the text.
Click on the image of code to increase the size and more clearly read the text.

In the case of an asynchronous request, the code structure is more complicated. When using the callback connection:didReceiveResponse:, the second parameter passed is NSURLResponse. You can send it to an NSHTTPURLResponse like so:

Click on the image of code to increase the size and more clearly read the text.
Click on the image of code to increase the size and more clearly read the text.

Comments

No comments yet.

Submit a Comment
Members and Guests

Sign in or sign up and post using a hubpages account.



    Like this Hub?
    Please wait working