How do you retrieve all HTTP headers from a NSURLRequest in Objective-C?
How do you retrieve all HTTP headers from a NSURLRequest in Objective-C?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
This falls under the easy, but not obvious class of iPhone programming problems. Worthy of a quick post:
The headers for an HTTP connection are included in the
NSHTTPURLResponseclass. If you have anNSHTTPURLResponsevariable you can easily get the headers out as aNSDictionaryby sending the allHeaderFields message.For synchronous requests — not recommended, because they block — it’s easy to populate an
NSHTTPURLResponse:With an asynchronous request you have to do a little more work. When the callback
connection:didReceiveResponse:is called, it is passed anNSURLResponseas the second parameter. You can cast it to anNSHTTPURLResponselike so: