AFNetworking 2.5如何为forHTTPHeaderField设置值

时间:2014-11-29 21:43:03

标签: ios xcode afnetworking

如何在AFNetworking“2.5”中执行以下操作。 我在较旧的AFNetworking版本中编写了此代码:

-(void)login{

    NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
                            username.text,@"username",password.text,@"password",nil];

    AFHTTPClient *httpClient= [[AFHTTPClient alloc]initWithBaseURL:[NSURL URLWithString:[NSString stringWithFormat:@"some_url"]]];

    [httpClient registerHTTPOperationClass:[AFJSONRequestOperation class]];
    [httpClient setDefaultHeader:@"Accept" value:@"application/json"];
    [httpClient setAuthorizationHeaderWithToken:@"Token"];

    NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST" path:@"some_path" parameters:params];

    [request addValue:@"some_value" forHTTPHeaderField:@"some_field"];

    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)
     {
         NSLog(@"Response- %@", responseObject);
     }
     failure:^(AFHTTPRequestOperation *operation, NSError *error)
     {
         NSLog(@"Error- %@",error);
     }];
    [operation start];
}

1 个答案:

答案 0 :(得分:3)

这就是我在AFNetworking 2.5中所做的。

-(void)login
{    
    NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys:
                        username.text,@"email",password.text,@"password",nil];

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager.requestSerializer setValue:some_value forHTTPHeaderField:@"some_field"];

    [manager POST:@"some_url" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) 
    {
        NSLog(@"JSON: %@", responseObject);
    } 
    failure:^(AFHTTPRequestOperation *operation, NSError *error) 
    {
        NSLog(@"Error: %@", error);
   }];
}