我想在我的项目中使用AFHTTPRequestOperationManager
。每个请求都应包含授权标头。否则服务器会报告错误。
好吧,我创建了一个AFHTTPRequestOperationManager
:
- (void) setManager:(AFHTTPRequestOperationManager *)manager
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_manager = [AFHTTPRequestOperationManager manager];
_manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain", nil];
_manager.operationQueue.maxConcurrentOperationCount = 1;
[_manager.requestSerializer setValue:@"XsziXjva92jAO29jng0wpBmKFJ92s9q" forHTTPHeaderField:@"Authorization"];
});
}
并在经理的帮助下发送请求:
+ (void) requestBoards
{
// show me my manager headers
DLog(@"%@", [[[[RequestManager instance] manager] requestSerializer] HTTPRequestHeaders]);
[[[RequestManager instance] manager] GET:URL_BOARDS parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
DLog(@"good");
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
// show me request headers
DLog(@"%@", operation.request.allHTTPHeaderFields);
}];
}
每个DLog
显示:
[RequestManager requestBoards]_block_invoke_2 [Line 66] {
"Accept-Language" = "en;q=1";
Authorization = XsziXjva92jAO29jng0wpBmKFJ92s9q;
"User-Agent" = "SITE/1.0 (iPhone Simulator; iOS 8.1; Scale/2.00)";
}
服务器响应不好:
{"data": {}, "error_text": "Key None is unauthorized, sorry :/", "success": false, "error": "unauthorized_key"}
我决定使用Wireshark检查我的请求,并且根本没有授权标头。我做错了什么?
添加了:
AFHTTPRequestOperationManager
仅发送授权标头。我已将标题标题更改为 AuthorizationLOL ,并且已成功发送