AFNetWorking 3.0之前设置请求头
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
[manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager.responseSerializer = [AFJSONResponseSerializer serializer];
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[manager.requestSerializer setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
-(AFHTTPSessionManager*)defaultHTTPManager
{
// 初始化对象
AFHTTPSessionManager*manager = [AFHTTPSessionManagermanager];
// 返回的格式 JSON
manager.responseSerializer= [AFJSONResponseSerializerserializer];
// 可接受的文本参数规格
manager.responseSerializer.acceptableContentTypes= [NSSetsetWithObjects:@"application/json",@"text/html",@"text/json",@"text/javascript",nilnil];
// 开始设置请求头
[manager.requestSerializersetValue:@"application/json"forHTTPHeaderField:@"Content-Type"];
[manager.requestSerializersetValue:@"填写系统版本"forHTTPHeaderField:@"sysVersion"];
[manager.requestSerializersetValue:@"填写设备"forHTTPHeaderField:@"deviceModel"];
[manager.requestSerializersetValue:@"填写App版本"forHTTPHeaderField:@"appVersion"];
// uid是每个用户对应的ID cipherText是密码
NSString* part1= [NSStringstringWithFormat:@"%lld:%@",uid,cipherText];
// 通过 ID :密码 的格式,用Basic 的方式拼接成字符串
NSString* authorization = [NSStringstringWithFormat:@"Basic %@",[part1base64Encode]];
// 设置Authorization的方法设置header
[manager.requestSerializersetValue:authorizationforHTTPHeaderField:@"Authorization"];
// 或者直接调用AF的方法进行设置
// - (void)setAuthorizationHeaderFieldWithUsername:(NSString *)username
// password:(NSString *)password
// {
// NSData *basicAuthCredentials = [[NSString stringWithFormat:@"%@:%@", username, password] dataUsingEncoding:NSUTF8StringEncoding];
// NSString *base64AuthCredentials = [basicAuthCredentials base64EncodedStringWithOptions:(NSDataBase64EncodingOptions)0];
// [self setValue:[NSString stringWithFormat:@"Basic %@", base64AuthCredentials] forHTTPHeaderField:@"Authorization"];
// }
returnmanager;
}
网友评论