AFNetworking 2.0向 GET 请求添加头文件

我刚开始使用 AFNetworking 2.0,我想知道我是如何将头文件放入 HTTP 获取请求的。文档设置了一个 GET,如下所示:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *parameters = @{@"foo": @"bar"};
[manager POST:@"http://example.com/resources.json" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];

但是由于我们不处理 NSURLRequests,我不确定如何设置 HTTP Header。

如果你能帮忙,我将不胜感激。
问候 ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~

70969 次浏览

AFNetworking 2.0的奇妙文档使得这个有点难找,但是它确实存在。AFHTTPRequestSerializer-setValue:forHTTPHeaderField:

或者,如果按照他们推荐的方法创建从 AFHTTPSessionManager派生的会话管理器,那么该类可以重写一个方法来修改每个请求 -dataTaskWithRequest:completionHandler:的头。我使用它来逐个检查请求和修改标题,并且更喜欢它而不是修改序列化器,因为它保持了包含在管理器中的网络责任(并且避免与单例混淆)

- (NSURLSessionDataTask *)dataTaskWithRequest:(NSURLRequest *)request completionHandler:(void (^)(NSURLResponse *, id, NSError *))completionHandler
{
static NSString *deviceId;
if(!deviceId)
{
deviceId = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
}


NSMutableURLRequest *req = (NSMutableURLRequest *)request;
// Give each request a unique ID for tracing
NSString *reqId = [NSString stringWithFormat:@"%@+%@", deviceId, [[NSUUID UUID] UUIDString] ];
[req setValue:reqId forHTTPHeaderField:"x-myapp-requestId"];
return [super dataTaskWithRequest:req completionHandler:completionHandler];
}

下面是使用 AFNetworking 2.0的示例

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager.requestSerializer setValue:@"calvinAndHobbesRock" forHTTPHeaderField:@"X-I do what I want"];


[manager GET:@"http://localhost:3000" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];

关键是以下两行:

manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager.requestSerializer setValue:@"calvinAndHobbessRock" forHTTPHeaderField:@"X-I-do-what-I-want"];

添加响应和请求序列化程序解决了我的问题。

manager.responseSerializer = [AFJSONResponseSerializer serializer];
manager.requestSerializer = [AFJSONRequestSerializer serializer];


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

我使用这个表单来使用特定的标题进行约会。

AFHTTPRequestOperationManager *operationManager = [AFHTTPRequestOperationManager manager];
[operationManager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[operationManager.requestSerializer setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];


[operationManager POST:url
parameters:params
success:^(AFHTTPRequestOperation *operation, id responseObject) {


if (success) {
success(responseObject);
}


}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {


NSLog(@"Error: %@", [error description]);


}
];

使用以下代码可以放置任何类型的头值:

[[FRHTTPReqManager sharedManager].requestSerializer setValue:value forHTTPHeaderField:key];

这是我认为最好的选择。在某个单例模式中,使用 NSURLSessionConfiguration配置一个 AFHTTPSessionManager,然后在每次需要发出请求时使用该 AFHTTPSessionManager

NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
config.HTTPAdditionalHeaders = @{@"Accepts": @"application/json"};


mySingletonSessionManager = [[AFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:kMyBaseUrl] sessionConfiguration:config];

我这么做... 是为了那些路过 Token 的人

[manager.requestSerializer setValue:[NSString stringWithFormat:@"Token token=\"%@\"", _userObj.oAuth] forHTTPHeaderField:@"Authorization"];