使用AFNetworking无法获得完整的响应标头

时间:2016-05-02 09:20:28

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

我最近刚开始使用AFNetworking,并且在接收来自基于JSON的服务器API的响应标头时遇到了问题。下面是我写的与服务器通信的代码。请求/响应是一个JSON对象。

问题:在阅读响应标头时,我的服务器未添加2个额外的安全密钥。当我使用传统的NSURLConnection或新的NSURLSession时,我能够获得它们,但是AFNetworking API并没有让它们回来。

有任何线索吗?

- (void)makeServerCall {
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

    // Set Request headers
    NSDictionary *securityHeaders = [MyUtilities securityRequestHeaders];

    for (NSString *headerKey in securityHeaders) {
        [manager.requestSerializer setValue:securityHeaders[headerKey] forHTTPHeaderField:headerKey];
    }

    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"application/json;charset=UTF-8"];

    [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [manager.requestSerializer setValue:@"gzip" forHTTPHeaderField:@"Accept-Encoding"];

    // Make Server Call
    [manager POST:self.urlString parameters:self.postBody success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        if (!self.skipSecurityHeaders) {
            [MyUtilities securityResponseHeaders:[operation.response allHeaderFields]];
        }

        NSLog(@"Received response for %@ \n Response Header %@ \n Response %@", self.urlString, [operation.response allHeaderFields], responseObject);

        [self didFinishWithResult1:responseObject andResponse:operation.response];
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        NSLog(@"Error: %@", error);
    }];
}

因此,当我打印[operation.response allHeaderFields]时,我得到以下输出:

Response Header {
    "Cache-Control" = "private, must-revalidate";
    "Content-Language" = "en-US";
    "Content-Length" = 929;
    "Content-Type" = "application/json;charset=UTF-8";
    Date = "Mon, 02 May 2016 09:12:52 GMT";
    Pragma = private;
    Server = "Apache-Coyote/1.1";
    "Set-Cookie" = "NSC_JOuoxqqabim3ljwdrzxad5c42hiv0bf=ffffffff12a12ecc45525d5f4f58455e445a4a42378b;path=/;secure;httponly";
}

缺少2个额外的密钥 - 我的服务器发送的key1key2

1 个答案:

答案 0 :(得分:0)

好的,想通了 - 我在设置requestSerializer之前设置了标题。所以流程应该是 -

- (void)makeServerCall {
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"application/json;charset=UTF-8"];

    [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [manager.requestSerializer setValue:@"gzip" forHTTPHeaderField:@"Accept-Encoding"];

    // Set Request headers
    NSDictionary *securityHeaders = [MyUtilities securityRequestHeaders];

    for (NSString *headerKey in securityHeaders) {
        [manager.requestSerializer setValue:securityHeaders[headerKey] forHTTPHeaderField:headerKey];
    }

    RUNNERLOG(@"Executing request for %@ \n Request Header %@ \n Request Body %@", self.urlString, securityHeaders, self.postBody);

    // Make Server Call
    [manager POST:self.urlString parameters:self.postBody success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        if (!self.skipSecurityHeaders) {
            [MyUtilities securityResponseHeaders:[operation.response allHeaderFields]];
        }

        NSLog(@"Received response for %@ \n Response Header %@ \n Response %@", self.urlString, [operation.response allHeaderFields], responseObject);

        [self didFinishWithResult1:responseObject andResponse:operation.response];
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        NSLog(@"Error: %@", error);
    }];
}