AFNetworking 2.0添加头到GET请求
我刚刚开始使用AFNetworking 2.0,我想知道如何将标题放入HTTP Get请求中。 文档build立一个像这样的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头。
任何帮助,将不胜感激。
问候,
麦克风
AFNetworking 2.0的梦幻般的文档使这个有点难找,但它在那里。 在AFHTTPRequestSerializer
是-setValue:forHTTPHeaderField:
另外,如果你按照他们推荐的方法创build一个派生自AFHTTPSessionManager
的会话pipe理器,那么这个类可以重写一个方法来修改每个请求上的-dataTaskWithRequest:completionHandler:
我使用它来检查请求,并在个案的基础上修改头文件,并且更喜欢修改序列化程序,因为它保持了pipe理程序中包含的networking连接的责任(并避免使用单例)
- (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:@"XI 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:@"XI-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]); } ];
这是我认为是最好的select。 在某个单例中,使用NSURLSessionConfiguration
configurationNSURLSessionConfiguration
,然后在每次发出请求时使用该AFHTTPSessionManager
。
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration]; config.HTTPAdditionalHeaders = @{@"Accepts": @"application/json"}; mySingletonSessionManager = [[AFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:kMyBaseUrl] sessionConfiguration:config];
我为…做了这些传递令牌的人
[manager.requestSerializer setValue:[NSString stringWithFormat:@"Token token=\"%@\"", _userObj.oAuth] forHTTPHeaderField:@"Authorization"];
使用下面的代码来放置任何types的标题值:
[[FRHTTPReqManager sharedManager].requestSerializer setValue:value forHTTPHeaderField:key];