工程中的请求,需要设置Header,请求令牌才访问,NSURLRequest 请求没有直接设置header 的方法,需要通过NSMutableURLRequest 来实现
一 NSURLRequest 设置
//1.创建requestNSURLRequest *request = [NSURLRequest requestWithURL:URL];//2.创建一个 NSMutableURLRequest 添加 headerNSMutableURLRequest *mutableRequest = [request mutableCopy];NSString *value = [NSString stringWithFormat:@"Bearer %@",[BZUserTool readToken]];[mutableRequest addValue:value forHTTPHeaderField:@"Authorization"];//3.把值覆给requestrequest = [mutableRequest copy];//4.查看请求头 NSLog(@"%@", request.allHTTPHeaderFields);
二 AFHTTPRequestOperationManager 设置
AFHTTPRequestOperationManager 设置比较简单
//1.获取一个管理者AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];//2.得到令牌NSString *value = [NSString stringWithFormat:@"Bearer %@",[BZUserTool readToken]];//3.设置[mgr.requestSerializer setValue:value forHTTPHeaderField:@"Authorization"];