Iphone 从NSURLConnection的响应头读取数据

Iphone 从NSURLConnection的响应头读取数据,iphone,objective-c,cocoa-touch,nsurlconnection,Iphone,Objective C,Cocoa Touch,Nsurlconnection,如何从服务器响应中发送的标头读取数据。我正在使用NSURLConnection发送请求。如果URL是HTTP URL,那么您在连接的代理的-connection:didReceiverResponse:方法(或通过另一个方法)中接收的NSHTTPURLResponse将是一个NSHTTPURLResponse,它有一个允许您访问标题的方法 NSURLResponse* response = // the response, from somewhere NSDictionary* headers

如何从服务器响应中发送的标头读取数据。我正在使用NSURLConnection发送请求。

如果URL是HTTP URL,那么您在连接的代理的
-connection:didReceiverResponse:
方法(或通过另一个方法)中接收的
NSHTTPURLResponse
将是一个
NSHTTPURLResponse,它有一个允许您访问标题的方法

NSURLResponse* response = // the response, from somewhere
NSDictionary* headers = [(NSHTTPURLResponse *)response allHeaderFields];
// now query `headers` for the header you want
就我而言

    NSHTTPURLResponse *response = ((NSHTTPURLResponse *)[task response]);
    NSDictionary *headers = [response allHeaderFields];
好办法

    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)[task response];
    if ([httpResponse respondsToSelector:@selector(allHeaderFields)]) {
         NSDictionary *dictionary = [httpResponse allHeaderFields];
         NSLog(@"%@", [dictionary description]);
    }