AFNetworking 2.0 //如何读取响应头

时间:2013-12-06 10:00:35

标签: ios objective-c cocoa-touch ios7 afnetworking-2

我正在使用新版本的AFNetworking,我无法弄清楚如何阅读响应的标题。 我正在使用AFHTTPSessionManager来执行我的查询,一切正常但我无法找到标题响应字段。

以下是我如何进行

     self.sessionManager = [[AFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:BASE_URL]];
     [self.sessionManager GET:urlId parameters:nil
                      success:^(NSURLSessionDataTask *task, id responseObject) {

                          if ([self.delegate respondsToSelector:@selector(userIsLoadedWithInfos:)]) {
                              [self.delegate userIsLoadedWithInfos: responseObject];
                          }
                      } failure:^(NSURLSessionDataTask *task, NSError *error) {
                          if ([self.delegate respondsToSelector:@selector(userLoadingFailed)]) {
                              [self.delegate userLoadingFailed];
                          }
                      }
         ];

我尝试读取任务的响应属性,但它返回一个不包含标题的NSURLResponse。 现在有人如何阅读2.0版本的响应标头?感谢

5 个答案:

答案 0 :(得分:14)

比Viruss mcs更健壮的代码:

if ([task.response isKindOfClass:[NSHTTPURLResponse class]]) {
    NSHTTPURLResponse *r = (NSHTTPURLResponse *)task.response;
    NSLog(@"%@" ,[r allHeaderFields]);
}

返回

{
    Connection = "Keep-Alive";
    "Content-Length" = 12771;
    "Content-Type" = "application/json";
    Date = "Fri, 06 Dec 2013 10:40:48 GMT";
    "Keep-Alive" = "timeout=5";
    "Proxy-Connection" = "Keep-Alive";
    Server = "gunicorn/18.0";
}

类似地,您可以确保使用[response respondsToSelector:@selector(allHeaderFields)]完成转换,但是在进行转换之前还应该调用

if ([task.response respondsToSelector:@selector(allHeaderFields)]) {
    NSHTTPURLResponse *r = (NSHTTPURLResponse *)task.response;
    NSLog(@"%@" ,[r allHeaderFields]);
}

或根本没有演员:

if ([task.response respondsToSelector:@selector(allHeaderFields)]) {
    NSLog(@"%@" ,[task.response performSelector:@selector(allHeaderFields)]);
}

答案 1 :(得分:13)

您是否尝试从NSURLResponse获取返回的标头

您可以尝试使用NSURLResponse对象,

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

希望这会帮助你。!

答案 2 :(得分:4)

有趣的是,上述回复表明参数id responseObject返回NSURLResponse。我在后端运行JAX-RS服务器,得到了不同的响应。对我的服务器执行curl命令时,我的回答如下:

$ curl -v "http://10.0.1.8:3000/items"
* About to connect() to 10.0.1.8 port 3000 (#0)
*   Trying 10.0.1.8...
* Adding handle: conn: 0x7f9f51804000
* Adding handle: send: 0
* Adding handle: recv: 0
* Curl_addHandleToPipeline: length: 1
* - Conn 0 (0x7f9f51804000) send_pipe: 1, recv_pipe: 0
* Connected to 10.0.1.8 (10.0.1.8) port 3000 (#0)
> GET /items HTTP/1.1
> User-Agent: curl/7.30.0
> Host: 10.0.1.8:3000
> Accept: */*
>
< HTTP/1.1 200 OK
< ETag: af0057e2-1c6d-4a47-b81a-a754238b60fd
< Content-Type: application/json
< Content-Length: 255
< Connection: keep-alive
<
* Connection #0 to host 10.0.1.8 left intact
[{"name":"Item1","uid":"F465AAD2-AA39-4C33-A57A-F0543C25C476"},
 {"name":"Item2","uid":"6505A82E-A473-4A7D-BC4B-BCBEFFFE8E9C"}]

我的responseObject是服务器响应正文中的项目数组,而不是NSURLResponse。以下是我检索标题的方法:

void (^handleSuccess)(NSURLSessionDataTask *, id) = ^(NSURLSessionDataTask *task, id responseObject) {
    // handle response headers
    NSHTTPURLResponse *response = ((NSHTTPURLResponse *)[task response]);
    NSDictionary *headers = [response allHeaderFields];

    // handle response body
    NSArray *responseItems = responseObject;
    for (NSDictionary *item in responseItems) {
        [self.activeDataController createObject:item];
    }
};

答案 3 :(得分:3)

我将AFHTTPRequestOperationManager子类化并使用:

- (AFHTTPRequestOperation *)POST:(NSString *)URLString
                      parameters:(NSDictionary *)parameters
                         success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
                         failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure;

我的大多数网络服务请求的方法。使用该方法时,响应头将成为操作对象的一部分。像这样:

[self POST:url parameters:newParams success:^(AFHTTPRequestOperation *operation, id responseObject) {
    // Response headers will be a dictionary
    NSDictionary *headers = operation.response.allHeaderFields;
...

答案 4 :(得分:0)

对于swift 2.0:

if let response = operation.response {
    print(response.allHeaderFields)
}