2017-03-31 36 views
1

有誰知道如何在AFHTTPSessionOperation中設置票證? 這是使用AFNetworking框架1.0如何使用AFHTTPSessionOperation或AFHTTPSessionManager設置票證?

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

但我不知道如何在以下使用AFNetworking框架3.1使用AFHTTPSessionOperation調用實現。

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

謝謝

回答

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出現在我的請求頭,符合市場預期:

enter image description here

我懷疑你的問題在於其他地方。此代碼確實將Authorization標頭設置爲ticket。確保這是設置ticket的正確位置。另外,請確保ticket是您認爲的。

+0

你是對的!問題來自我的票證,它沒有正確的數據。 – Aviva

相關問題