有谁知道如何在AFHTTPSessionOperation内设置票证? 这是使用AFNetworking framework 1.0的先前调用
NSURLRequest* request = [self.myClient requestWithMethod:@"POST" path:[NSString stringWithFormat:@"%@/%@", controller, action] parameters:parameters];
AFHTTPRequestOperation* operation = [self.myClient HTTPRequestOperationWithRequest:request success:success failure:failure];
[self.mirrorClient enqueueHTTPRequestOperation:operation];
故障单存储在self.myClient中。 self.myClient.ticket
但是我不确定如何在AFHTTPSessionOperation与AFNetworking framework 3.1的下一次调用中实现它。
AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] init];
AFHTTPRequestSerializer <AFURLRequestSerialization> * requestSerializer = manager.requestSerializer;
[requestSerializer setValue:[NSString stringWithFormat:@"%@", self.myClient.ticket] forHTTPHeaderField:@"Authorization"];
NSOperation *operation = [AFHTTPSessionOperation operationWithManager:manager HTTPMethod:@"POST"
URLString:urlString parameters:parameters
uploadProgress:nil downloadProgress: nil
success:success failure:failure];
谢谢
答案 0 :(得分:1)
此代码看起来基本正确。您可以简化requestSerializer
配置,我可能不会为每个请求实例化一个新会话,但以下工作对我来说很好:
- (void)performRequest:(NSString *)urlString
parameters:(id)parameters
success:(nullable void (^)(NSURLSessionDataTask *task, id responseObject))success
failure:(nullable void (^)(NSURLSessionDataTask *task, NSError *error))failure {
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager.requestSerializer setValue:self.myClient.ticket forHTTPHeaderField:@"Authorization"];
NSOperation *operation = [AFHTTPSessionOperation operationWithManager:manager
HTTPMethod:@"POST"
URLString:urlString
parameters:parameters
uploadProgress:nil
downloadProgress:nil
success:success
failure:failure];
[self.queue addOperation:operation];
}
我在Charles中看了它,并且票证12345678
出现在我的请求标题中,正如所料:
我怀疑你的问题在别处。此代码会将Authorization
标头设置为ticket
。确保这是设置ticket
的正确位置。另外,请确保ticket
符合您的预期。