ios - AFNetworking:设置 GET 参数 * 和 * 拦截重定向

标签 ios afnetworking

我在 iOS 项目中使用 AFNetworking 2.0,我正在尝试使用一些参数构建 GET 请求,拦截重定向。

我看到了方法 -[AFHTTPRequestOperation setRedirectResponseBlock],我用它来获取重定向并对它们进行处理。但我看不到如何在该操作上设置请求参数。这是它的样子:

    AFHTTPRequestOperation *ballotOperation = [[AFHTTPRequestOperation alloc] initWithRequest:urlRequest];

    [ballotOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"in completion");
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"in error");
    }];

    [ballotOperation setRedirectResponseBlock:^NSURLRequest *(NSURLConnection *connection, NSURLRequest *request, NSURLResponse *redirectResponse) {
        if (redirectResponse == nil) {
            return request;
        } else {
            NSLog(@"in redirect, blocking");
            [ballotOperation cancel];
            return nil;
        }
    }];

    [[AFHTTPRequestOperationManager manager].operationQueue addOperation:ballotOperation];

我看到 AFHTTRequestOperationManager 有方法 GET:parameters:success:failure:,您可以在其中设置参数。但这会立即启动请求,而不是让我有机会在其上设置重定向 block 。

我看到一些示例代码来自 AFNetworking 1.x 使用 AFHTTPClient,但我不想回去!

我怎样才能做我想做的事?

最佳答案

AFHTTPRequestOperationManager.m 中的[AFHTTPRequestOperationManager GET...] 方法只是创建一个AFHTTPRequestOperation 对象并将其添加到操作队列。以此为例,您可以完成您想要做的事情。

这是在 AFHTTPRequestOperationManagerGET 方法中创建请求的方式:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSMutableURLRequest *request = [manager.requestSerializer requestWithMethod:@"GET" URLString:[[NSURL URLWithString:URLString relativeToURL:manager.baseURL] absoluteString] parameters:parameters error:nil];

其中 urlString 是一个代表 url 的 NSString,参数是一个 NSDictionary。

我相信您的其余代码应该可以工作,但为了以防万一,以下是在 GET 方法中的完成方式(同时添加了重定向 block ):

AFHTTPRequestOperation *ballotOperation = [self HTTPRequestOperationWithRequest:request success:success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"in completion");
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"in failure");
}];

[ballotOperation setRedirectResponseBlock:^NSURLRequest *(NSURLConnection *connection, NSURLRequest *request, NSURLResponse *redirectResponse) {
    if (redirectResponse == nil) {
        return request;
    } else {
        NSLog(@"in redirect, blocking");
        [ballotOperation cancel];
        return nil;
    }
}];

[manager.operationQueue addOperation:ballotOperation];

关于ios - AFNetworking:设置 GET 参数 * 和 * 拦截重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21131488/

相关文章:

ios - 为什么 GCDAsyncSocket 总是在读取超时后断开连接?

ios - UIApplication 发送操作 :to:from:forEvent: iOS7 no user code

ios - 使用 AFNetworking 保存/缓存文件并将其用于在线和离线访问

ios - 删除图像缓存

ios - 如何将数据从弹出框内的 uicollectionview 中的按钮单击发送到主视图 Controller 中的表格单元格?

ios - 如何将 .wav 文件转换为 .caf 文件以便在 iOS 中使用

ios - 谷歌地图自动完成改变颜色

ios - "AFNetworking.h".pch 文件中找不到文件

TableView Controller 中的 iOS/AFNetworking TableView 不显示从响应对象填充的数组中的任何数据

php - 秦牛:始终返回“405不允许”