如何从服务器响应中发送的标头中读取数据。我正在使用NSURLConnection发送请求。
答案 0 :(得分:73)
如果网址是HTTP网址,那么您在连接的委托的NSURLResponse
方法(或通过其他方法)中收到的-connection:didReceiveResponse:
将为NSHTTPURLResponse
,其中包含{ {3}}方法,可让您访问标题。
NSURLResponse* response = // the response, from somewhere
NSDictionary* headers = [(NSHTTPURLResponse *)response allHeaderFields];
// now query `headers` for the header you want
答案 1 :(得分:3)
就我而言
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]);
}