Redirect AFN网络:防止重定向
我目前正在使用AFNetworking发送HTTP请求。服务器当前设置为在失败和成功场景中重定向到另一个HTML页面来响应(我们的客户机控制着服务器,我们无法在没有大量官僚主义的情况下改变其行为) 我需要捕捉它何时重定向到successhtml页面,而不是由于重定向而自动发布另一个HTTP请求Redirect AFN网络:防止重定向,redirect,afnetworking,Redirect,Afnetworking,我目前正在使用AFNetworking发送HTTP请求。服务器当前设置为在失败和成功场景中重定向到另一个HTML页面来响应(我们的客户机控制着服务器,我们无法在没有大量官僚主义的情况下改变其行为) 我需要捕捉它何时重定向到successhtml页面,而不是由于重定向而自动发布另一个HTTP请求 - (void) fetchHttpRequest: (void (^)(id result)) completionBlock errorBlock: (void (^)(
- (void) fetchHttpRequest: (void (^)(id result)) completionBlock
errorBlock: (void (^)(NSError * error, id result)) errorBlock
{
NSMutableURLRequest * urlRequest = [NSMutableURLRequest requestWithURL:
[NSURL URLWithString:@"/"]];// replace with correct url
AFHTTPRequestOperation * operation = [[AFHTTPRequestOperation alloc]
initWithRequest:urlRequest];
[operation setCompletionBlockWithSuccess:
^(AFHTTPRequestOperation * operation, id result)
{
completionBlock(result);
}
failure:
^(AFHTTPRequestOperation * operation, NSError *error)
{
errorBlock(error, nil);
}];
// check the redirection
[operation setRedirectResponseBlock:
^NSURLRequest *(NSURLConnection *connection, NSURLRequest *request,
NSURLResponse *redirectResponse)
{
NSString * reqUrlPath = @"/";// replace with success URL
if ([reqUrlPath isEqualToString:[[request URL] path]])
{
// success case
return nil;
}
else
{
// failure case
return request;
}
}];
[operation start];
}
在应用程序被重定向之前,操作的完成块不会捕获任何成功,这会导致失败
它确实到达了重定向块中的//success case
,但由于所需的返回类型,我无法阻止代码尝试转到该位置的下一页。因此,在我定义completionBlock
的地方,它会收到一个错误而不是一个完成