【问题标题】:Reading data from response header of NSURLConnection从 NSURLConnection 的响应头中读取数据
【发布时间】:2011-06-08 00:34:57
【问题描述】:

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

【问题讨论】:

    标签: iphone objective-c cocoa-touch nsurlconnection


    【解决方案1】:

    如果 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
    

    【讨论】:

      【解决方案2】:

      就我而言

          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]);
          }
      

      【讨论】:

        猜你喜欢
        • 2016-09-02
        • 2019-11-26
        • 1970-01-01
        • 2017-10-17
        • 1970-01-01
        • 1970-01-01
        • 2012-05-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多