AFNetworking 2.0请求w。自定义标题 [英] AFNetworking 2.0 Request w. Custom Header
问题描述
AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
NSDictionary * parameters = @ {@uid:@1};
AFHT
[manager POST:@http://myface.com/api/profile/format/jsonparameters:parameters success:^(AFHTTPRequestOperation * operation,id responseObject){
// NSLog(@JSON:%@,responseObject);
self.feedArray = [responseObject objectForKey:@feed];
[self.tableView reloadData];
$ b}失败:^(AFHTTPRequestOperation *操作,NSError *错误){
NSLog(@错误:%@,错误);
}];
> AFHTTPRequestOperationManager 你会注意到一个名为 requestSerializer
的属性。这是 AFHTTPRequestSerializer
类型,并且通过HTTP管理器创建的请求使用由此对象指定的标头构建。
所以,试试这个:
AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
[manager.requestSerializer setValue:@SomeValueforHTTPHeaderField:@SomeHeaderField]
//发出请求
您可以从请求序列化器中读取标题字典,如下所示:
manager.requestSerializer.HTTPRequestHeaders
请注意,一旦您以这种方式设置标题,用于所有其他操作!
Ive tried to avoid asking such a newb question on here, but im a Android dev learning IOS and I cant figure out for the life of me how to add a simple header to my post requests using AFNetworking 2.0. Below is my code so far which works if i want to make a request that doesnt require a header.Could anyone show me via adding to my snippet or providing a alternate one that does this? I came across this tut: http://www.raywenderlich.com/30445 that shows how to add a header under the "A RESTful Class" heading , but its for afnetworking 1.0 and is now depreciated as far as i can tell.
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *parameters = @{@"uid": @"1"};
AFHT
[manager POST:@"http://myface.com/api/profile/format/json" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
//NSLog(@"JSON: %@", responseObject);
self.feedArray = [responseObject objectForKey:@"feed"];
[self.tableView reloadData];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
} ];
}
Under AFHTTPRequestOperationManager
you will notice a property called requestSerializer
. This is of type AFHTTPRequestSerializer
, and requests made through the HTTP manager are constructed with the headers specified by this object.
So, try this:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager.requestSerializer setValue:@"SomeValue" forHTTPHeaderField:@"SomeHeaderField"]
//Make your requests
You can read the headers dictionary from the request serializer as follows:
manager.requestSerializer.HTTPRequestHeaders
Note that once you set a header this way, it will be used for all other operations!
这篇关于AFNetworking 2.0请求w。自定义标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!