Re: Getting the content-type in a HTTP response
Re: Getting the content-type in a HTTP response
- Subject: Re: Getting the content-type in a HTTP response
- From: Bill Garrison <email@hidden>
- Date: Fri, 25 Jan 2008 18:35:38 -0500
On Jan 25, 2008, at 5:44 PM, Dave Carrigan wrote:
I need to get the content-type of a HTTP response. My delegate code
looks like:
- (void)connection:(NSURLConnection*)connection didReceiveResponse:
(NSURLResponse*)theResponse
{
NSString* content_type = [[(NSHTTPURLResponse*)theResponse
allHeaderFields] valueForKey:@"content-type"];
}
It doesn't work. The reason it doesn't work is because the server is
returning the header as "Content-Type", and the dictionary returned
by allHeaderFields method has case-sensitive keys, even though the
HTTP spec clearly says that HTTP header names are case-insensitive.
Is there something I'm missing, or was Apple really this
shortsighted in designing this API?
The key for accessing values in the allHeaderFields dictionary is not
case-insensitive. That's what you're hitting up against. If the key
for an NSDictionary is an NSString, that string instance has to be
treated as case-sensitive.
Typed in Mail...
// Get the content type in a way that respects the case-insensitivity
of the HTTP spec regarding header field keys
{
NSString *contentTypeValue = nil;
for ( NSString *headerKey in [[(NSHTTPURLResponse*)theResponse
allHeaderFields] allKeys] ) {
if ( [@"content-type" caseInsensitiveCompare:headerKey] ==
NSOrderedSame ) {
contentTypeValue = [(NSHTTPURLResponse*)theResponse
allHeaderFields] valueForKey: headerKey];
}
}
}
I guess you'd have to do something similar with every HTTP header key.
Bill
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden