AFHTTPSessionManager在重定向时设置授权标头

时间:2015-09-03 08:34:30

标签: ios afnetworking

我正在向OAuth服务器发出请求,该请求会将请求重定向到其他网址,然后不会同时传递授权标头。

我需要为所有请求实现重定向。之前我使用AFHTTPRequestOperation API使用以下代码执行相同的操作:

- (AFHTTPRequestOperation *)request:(TMERequest)request
                     parameters:(NSDictionary *)parameters
                        makeURL:(NSString *(^)(NSString *URL))URLBlock
                 loadingMessage:(NSString *)message
                     completion:(void (^)())completionBlock
                        success:(void (^)(id reponse))successBlock
                        failure:(void (^)(NSError *error))failureBlock     {
    AFHTTPRequestOperation *operation = [super
                                     request:request
                                     parameters:parameters
                                     makeURL:URLBlock
                                     loadingMessage:message
                                     completion:completionBlock
                                     success:successBlock
                                     failure:failureBlock];
    if (self.authorizationToken) {
        //Redirect clears the Authorization header, this is to add it back
        [operation setRedirectResponseBlock:^NSURLRequest *(NSURLConnection *connection, NSURLRequest *request, NSURLResponse *redirectResponse) {
            if (request.allHTTPHeaderFields[kAuthorizationHeader]) {
                return request;
            }
            NSMutableURLRequest *authorizedRequest = [request mutableCopy];
            [authorizedRequest setValue:[NSString stringWithFormat:kAuthorizationFormat, self.authorizationToken] forHTTPHeaderField:kAuthorizationHeader];
            return [authorizedRequest copy];
         }];
    }
    return operation;
}

如何使用AFHTTPSessionManager

实现此目的

0 个答案:

没有答案