建议使用AFNetworking 2.0拦截和阻止重定向的方法是什么? [英] What is the recommended way to intercept and prevent redirects with AFNetworking 2.0?

查看:524
本文介绍了建议使用AFNetworking 2.0拦截和阻止重定向的方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我看来,执行此操作的正确位置是在 AFURLSessionManager 中,设置 taskWillPerformHTTPRedirection 块,但我不确定处理该块的最佳方法

It seems to me that the proper place to do this is in AFURLSessionManager, in setting the taskWillPerformHTTPRedirection block, but I am unsure of the best way to handle it.

当前,在我的 AFHTTPSessionManager 子类中,我正在为所有请求全局设置重定向块,并且我知道我可以通过在此处返回 nil 来防止重定向:

Currently, in my AFHTTPSessionManager subclass, I am setting the redirect block globally for all requests, and I know I can prevent redirects by returning nil here:

- (void)setupRedirectBlock {
    [self setTaskWillPerformHTTPRedirectionBlock:^NSURLRequest *(NSURLSession *session, NSURLSessionTask *task, NSURLResponse *response, NSURLRequest *request) {
        return nil;
    }];
}

...但是我只需要在特定任务上执行此操作似乎不是从任务本身获取此信息的方法。

...but I need to only do this on specific tasks, and there doesn't appear to be a way to get this information from the task itself.

我想我正在寻找某种用户信息字典或可以用来做的事情设置一个标记,告诉该方法返回请求或返回 nil 。当前,看来我必须在客户端中的响应/请求URL上进行字符串比较,该URL与实际创建任务和路径的位置相距甚远。

I guess I am looking for some sort of user info dictionary or something I can use to set a flag telling this method to either return the request or return nil. Currently, it looks like I would have to do a string comparison on the response/request URL in the client where it is far away from where the task and path is actually created.

所以这引出了一个问题,我是在与常规作斗争吗,还是真的没有更好的方法来逐个任务地拦截AFNetworking 2.0重定向?

So this begs the question, am I fighting convention, or is there really no better way to intercept an AFNetworking 2.0 redirect on a task-by-task basis?

推荐答案

setTaskWillPerformHTTPRedirectionBlock 是拦截重定向的最佳方法。会话管理器负责根据与任务关联的请求确定何时或何时不阻止重定向。在大多数情况下,请求的路径应该足够确定,但是用户可以在自定义请求标头字段中另外标记信息。

setTaskWillPerformHTTPRedirectionBlock is the best way to intercept redirects. The session manager is responsible for determining when or when not to prevent redirects based on the request associated with the task. In most cases, the path of the request should be a sufficient determination, but the user could additionally tag information in a custom request header field.

这篇关于建议使用AFNetworking 2.0拦截和阻止重定向的方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆