79

我刚刚开始使用 AFNetworking 2.0,我想知道如何将标头放入 HTTP Get 请求中。文档设置了这样的 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 标头。

任何帮助,将不胜感激。
问候,
迈克

4

7 回答 7

147

这是一个使用 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"];
于 2013-12-03T05:35:50.917 回答
84

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];
}
于 2013-10-19T14:12:11.633 回答
17

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

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

[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
于 2014-04-03T23:06:38.000 回答
7

我已使用此表格与特定标题进行预约。

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]);

               }
 ];
于 2014-06-26T21:41:24.003 回答
6

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

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

mySingletonSessionManager = [[AFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:kMyBaseUrl] sessionConfiguration:config];
于 2014-08-27T03:07:33.937 回答
3

我这样做了......对于那些传递令牌的人

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

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

[[FRHTTPReqManager sharedManager].requestSerializer setValue:value forHTTPHeaderField:key];
于 2014-07-11T07:42:55.440 回答