其实应该就这么简单
NSString* contentType = [[(NSHTTPURLResponse*)theResponse allHeaderFields] valueForKey:@"content-type"];
或者
NSString* contentType = [[(NSHTTPURLResponse*)theResponse allHeaderFields][@"content-type"]];
但问题是响应可能会以大写或小写形式返回键的名称,而 NSDictionary 对键确实区分大小写,因此您应该对键进行不区分大小写的搜索
NSDictionary* allFields = [[(NSHTTPURLResponse*)theResponse allHeaderFields];
NSString* contentType;
for (NSString* key in allFields.allKeys) {
if ([key compare:@"content-type" options:NSCaseInsensitiveSearch] == NSOrderedSame) {
// This is it
contentType = allFields[key];
break;
}
}