How to set ticket with AFHTTPSessionOperation or AFHTTPSessionManager?

試著忘記壹切 提交于 2019-12-24 07:15:06

问题


Does anyone know how to set ticket inside AFHTTPSessionOperation? This is the previous call using 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];

The ticket is stored inside the self.myClient. self.myClient.ticket

But I'm not sure how to implement that in the following call using AFHTTPSessionOperation with 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];

Thank you


回答1:


This code looks basically correct. You could simplify the requestSerializer configuration a tad, and I might not instantiate a new session for every request, but the following worked fine for me:

- (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];
}

I watched it in Charles, and the ticket, 12345678 appeared in my request header, as expected:

I suspect your problem rests elsewhere. This code does set the Authorization header to ticket. Make sure this is the right place to set the ticket. Also, make sure the ticket is what you think it is.



来源:https://stackoverflow.com/questions/43149842/how-to-set-ticket-with-afhttpsessionoperation-or-afhttpsessionmanager

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!