aspcartman
aspcartman

Reputation: 225

Getting "Location" header from NSHTTPURLResponse

Can't get "Location" header from response at all. Wireshark says that i've got one:

Location: http://*/index.html#0;sid=865a84f0212a3a35d8e9d5f68398e535

But

NSHTTPURLResponse *hr = (NSHTTPURLResponse*)response;
NSDictionary *dict = [hr allHeaderFields];          
NSLog(@"HEADERS : %@",[dict description]);

Produces this:

HEADERS : {
    Connection = "keep-alive";
    "Content-Encoding" = gzip;
    "Content-Type" = "text/html";
    Date = "Thu, 25 Mar 2010 08:12:08 GMT";
    "Last-Modified" = "Sat, 29 Nov 2008 15:50:54 GMT";
    Server = "nginx/0.7.59";
    "Transfer-Encoding" = Identity;
}

No location anywhere. How to get it? I need this "sid" thing.

Upvotes: 6

Views: 6474

Answers (2)

Max MacLeod
Max MacLeod

Reputation: 26662

Thought it might be helpful to supplement @Rob Keniger's answer with an example of how to access the Location header in Swift. As shown above, "Location" is an HTTP response header field, so in Swift this can be done as follows:

if let location = response.allHeaderFields["Location"] as? String
{
    NSLog("Location \(location)")
}

where response is of type NSHTTPURLResponse. See NSHTTPURLResponse Class Reference.

Upvotes: 3

Rob Keniger
Rob Keniger

Reputation: 46020

NSHTTPURLResponse is a subclass of NSURLResponse so you can just ask it for its URL. This returns an NSURL object from which you can get the URL components such as the query, parameter string or fragment. In your case you want the fragment component:

NSURL* url = [response URL];
NSString* fragment = [url fragment];
//fragment should be: 0;sid=865a84f0212a3a35d8e9d5f68398e535

Upvotes: 5

Related Questions