NSHTTPURLResponse Class Reference

Inherits from
Conforms to
Framework
/System/Library/Frameworks/Foundation.framework
Availability
Available in OS X v10.2 with Safari 1.0 installed.
Available in OS X v10.2.7 and later.
Companion guide
Declared in
NSURLResponse.h
Related sample code

Overview

The NSHTTPURLResponse class is a subclass of NSURLResponse that provides methods for accessing information specific to HTTP protocol responses. Whenever you make HTTP URL load requests, any response objects you get back from the NSURLSession, NSURLConnection, or NSURLDownload class are instances of the NSHTTPURLResponse class.

Adopted Protocols

NSCoding
NSCopying

Tasks

Initializing a Reponse Object

Getting HTTP Response Headers

Getting Response Status Code

Class Methods

localizedStringForStatusCode:

Returns a localized string corresponding to a specified HTTP status code.

+ (NSString *)localizedStringForStatusCode:(NSInteger)statusCode
Parameters
statusCode

The HTTP status code. See RFC 2616 for details.

Return Value

A localized string suitable for displaying to users that describes the specified status code.

Availability
  • Available in OS X v10.2 with Safari 1.0 installed.
  • Available in OS X v10.2.7 and later.
Declared In
NSURLResponse.h

Instance Methods

allHeaderFields

Returns all the HTTP header fields of the receiver.

- (NSDictionary *)allHeaderFields
Return Value

A dictionary containing all the HTTP header fields received as part of the server’s response. By examining this dictionary clients can see the “raw” header information returned by the HTTP server.

The keys in this dictionary are the header field names, as received from the server. See RFC 2616 for a list of commonly used HTTP header fields.

Discussion

HTTP headers are case insensitive. To simplify your code, certain header field names are canonicalized into their standard form. For example, if the server sends a content-length header, it is automatically adjusted to be Content-Length.

The returned dictionary of headers is configured to be case-preserving during the set operation (unless the key already exists with a different case), and case-insensitive when looking up keys.

For example, if you set the header X-foo, and then later set the header X-Foo, the dictionary’s key will be X-foo, but the value will taken from the X-Foo header.

Special Considerations

Prior to OS X v10.7 and iOS 5, canonicalization occurred for all header fields. The case-preserving dictionary was first introduced in OS X v10.7.2 and iOS 5.

Availability
  • Available in OS X v10.2 with Safari 1.0 installed.
  • Available in OS X v10.2.7 and later.
Declared In
NSURLResponse.h

initWithURL:statusCode:HTTPVersion:headerFields:

Initializes an HTTP URL response object with a status code, protocol version, and response headers.

- (id)initWithURL:(NSURL *)url statusCode:(NSInteger)statusCode HTTPVersion:(NSString *)HTTPVersion headerFields:(NSDictionary *)headerFields
Parameters
url

The URL from which the response was generated.

statusCode

The HTTP status code to return (404, for example). See RFC 2616 for details.

HTTPVersion

The version of the HTTP response as returned by the server. This is typically represented as "HTTP/1.1".

headerFields

A dictionary representing the keys and values from the server’s response header.

Return Value

An initialized NSHTTPURLResponse object or nil if an error occurred during initialization.

Availability
  • Available in OS X v10.7 and later.
Declared In
NSURLResponse.h

statusCode

Returns the receiver’s HTTP status code.

- (NSInteger)statusCode
Return Value

The receiver’s HTTP status code. See RFC 2616 for details.

Availability
  • Available in OS X v10.2 with Safari 1.0 installed.
  • Available in OS X v10.2.7 and later.
Declared In
NSURLResponse.h