AFNetworking3.0添加请求头Header

2016-05-09  本文已影响6525人  禾子_____

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

manager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];

//不设置会报-1016或者会有编码问题

manager.responseSerializer = [AFHTTPResponseSerializer serializer];

//不设置会报 error 3840

manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];

//创建你得请求url、设置请求头

NSString *urlString = [NSString stringWithFormat:@"http://xxxxx"];

NSMutableURLRequest *request = [[AFJSONRequestSerializer serializer] requestWithMethod:@"POST" URLString:urlString parameters:你的参数 error:nil];

[request addValue:你需要的accept-id forHTTPHeaderField:@"Accept-Id"];

[request addValue:你需要的user-agent forHTTPHeaderField:@"User-Agent"];

NSData *body = 你需要提交的data;

[request setHTTPBody:body];

//发起请求

[[manager dataTaskWithRequest:request completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {

if (!error) {

NSLog(@"Reply JSON: %@", responseObject);

if ([responseObject isKindOfClass:[NSDictionary class]]) {

//处理你的数据

}

} else {

NSLog(@"Error: %@, %@, %@", error, response, responseObject);

}

}] resume]; 

上一篇下一篇

猜你喜欢

热点阅读