使用 AFNetworking 未获得完整的响应标头
Posted
技术标签:
【中文标题】使用 AFNetworking 未获得完整的响应标头【英文标题】:Not getting complete response headers with AFNetworking 【发布时间】:2016-05-02 09:20:27 【问题描述】:我最近才开始使用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 个额外的密钥 - 我的服务器发送的 key1
和 key2
。
【问题讨论】:
【参考方案1】:好的,想通了 - 我在设置 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);
];
【讨论】:
以上是关于使用 AFNetworking 未获得完整的响应标头的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 JSON 格式解析通过 AFNetworking 1.0 获得的响应
如何从服务器获取 AFNetworking 中的完整响应字符串?
TableView 未使用来自 AFNetworking 响应对象的新数据重新加载