AFNetworking将重定向请求从GET更改为POST

时间:2015-03-18 19:58:49

标签: ios xcode redirect afnetworking afnetworking-2

所以,我正在从使用ASI库切换到AFNetworking,我遇到了重定向请求的问题。这是我正在使用的代码:

    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:originalRequest];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    debugLog(@"SM Success, Redirect URL: %@",[[[operation response] URL] absoluteString]);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    debugLog(@"SM Fail: %@", error);
}];

[operation setRedirectResponseBlock:^NSURLRequest *(NSURLConnection *connection, NSURLRequest *request, NSURLResponse *redirectResponse) {

    if (redirectResponse) {
        debugLog(@"REDIRECT URL: @%", [request URL]);
        NSMutableURLRequest *r = [originalRequest mutableCopy];
        [r setURL: [request URL]];
        return r;
    } else {
        debugLog(@"Redirecting to : %@", [request URL]);
        return request;
    }        
}];

我想要做的是将重定向请求从GET更改为POST。在ASI中,我明确地禁止了所有重定向,一旦我收到302状态,我就会创建一个新请求并将其作为POST发送。这与AFNetworking应该做的一样吗?

1 个答案:

答案 0 :(得分:2)

复制原始请求时,您使用的是相同的HTTP方法,如果要从GET更改为POST,则应执行以下操作:

NSMutableURLRequest *r = [originalRequest mutableCopy];
[r setHTTPMethod:@"POST"];
[r setURL: [request URL]];
return r;