【问题标题】:AFNetworking 2.5 How to set value for forHTTPHeaderFieldAFNetworking 2.5 如何为 HTTPHeaderField 设置值
【发布时间】:2014-11-29 21:43:03
【问题描述】:

如何在 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];
}

【问题讨论】:

标签: ios xcode afnetworking


【解决方案1】:

这就是我在 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);
   }];
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多