【发布时间】:2011-06-08 00:34:57
【问题描述】:
如何从服务器响应中发送的标头中读取数据。我正在使用 NSURLConnection 发送请求。
【问题讨论】:
标签: iphone objective-c cocoa-touch nsurlconnection
如何从服务器响应中发送的标头中读取数据。我正在使用 NSURLConnection 发送请求。
【问题讨论】:
标签: iphone objective-c cocoa-touch nsurlconnection
如果 URL 是 HTTP URL,那么您在连接代理的 -connection:didReceiveResponse: 方法(或通过其他方法)中收到的 NSURLResponse 将是 NSHTTPURLResponse,它有一个 -allHeaderFields 方法,可以让您访问标题。
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]);
}
【讨论】: