19

嗨,我有以下代码访问 URL:

NSString * stringURL = [NSString stringWithFormat:@"%@/%@/someAPI", kSERVICE_URL, kSERVICE_VERSION];
NSURLRequest * request = [NSURLRequest requestWithURL:[NSURL URLWithString:stringURL]];

AFJSONRequestOperation * operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
    completionHandler(JSON, nil);
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
    completionHandler(nil, error);
}];

但我想将用户令牌作为参数传递给HEADER,例如X-USER-TOKEN.

找不到AFNetworking documentation,要不要改操作类型?

4

4 回答 4

30

使用AFHTTPClient或继承它!

您可以像这样设置默认标题-setDefaultHeader:value:

[self setDefaultHeader:@"X-USER-TOKEN" value:userToken];

您可以查看文档

于 2013-02-28T16:55:20.073 回答
28
NSMutableURLRequest* request = [[NSMutableURLRequest alloc] initWithURL:url];

[request setValue: @"X-USER-TOKEN"  forHTTPHeaderField:@"< clientToken >"];

[AFJSONRequestOperation JSONRequestOperationWithRequest: request ...]
于 2013-02-28T16:58:57.963 回答
8

我这样做了:)

[manager.requestSerializer setValue:[NSString stringWithFormat:@"Token token=\"%@\"", _userObj.oAuth] forHTTPHeaderField:@"Authorization"];
于 2015-05-21T03:28:36.430 回答
3

如果你有一个抽象层,比如说 APIManager,那么你应该在一个特定的方法中执行以下操作

 [[HTTPClient sharedHTTPClient].requestSerializer setValue:YOUR_KEY forHTTPHeaderField:@"X-Register"];
于 2014-09-24T23:56:00.443 回答