AFNetworking 处理请求头和请求体
2017-04-10 本文已影响543人
打不死的小怪兽
首先是请求的URL
NSString *url = @"your url";
接下来定义AFHTTPSessionManager,
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
设置相关的请求头,通过requestSerializer来实现,可以根据自己的需求(定义一个或者多个请求头),
[manager.requestSerializer setValue:@"value1" forHTTPHeaderField:@"key1"];
[manager.requestSerializer setValue:@"value2" forHTTPHeaderField:@"key2"];
......
实现带请求体的POST方法,
[manager POST:url
parameters:@{
@"key1":@"value1",
@"key2":@"value2"
......
}
progress:nil
success:^(NSURLSessionTask *task, id responseObject) {
NSLog("response:%@", responseObject);
}
}
failure:^(NSURLSessionTask *task, NSError *error) {
NSLog(@"error:%@", error);
}];