网络请求设置AuthorizationHeader

-(AFHTTPSessionManager*)defaultHTTPManager{

// 初始化对象

AFHTTPSessionManager*manager=[AFHTTPSessionManager manager];

// 返回的格式

JSONmanager.responseSerializer=[AFJSONResponseSerializer serializer];

// 可接受的文本参数规格

manager.responseSerializer.acceptableContentTypes=[NSSet setWithObjects:@"application/json",@"text/html",@"text/json",@"text/javascript",nil];

// 开始设置请求头

[manager.requestSerializer setValue:@"application/json"forHTTPHeaderField:@"Content-Type"];[manager.requestSerializer setValue:@"填写系统版本"forHTTPHeaderField:@"sysVersion"];[manager.requestSerializer setValue:@"填写设备"forHTTPHeaderField:@"deviceModel"];[manager.requestSerializer setValue:@"填写App版本"forHTTPHeaderField:@"appVersion"];

// uid是每个用户对应的ID cipherText是密码NSString*part1=[NSString stringWithFormat:@"%lld:%@",uid,cipherText];

// 通过 ID :密码 的格式,用Basic 的方式拼接成字符串NSString*authorization=[NSString stringWithFormat:@"Basic %@",[part1 base64Encode]];

// 设置Authorization的方法设置header[manager.requestSerializer setValue:authorization forHTTPHeaderField:@"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;}

你可能感兴趣的:(网络请求设置AuthorizationHeader)