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

AFNetworking change redirect request from GET to POST

所以,我正在从使用 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 做的一样吗?

复制原始请求时,您使用的是相同的 HTTP 方法,如果您想从 GET 更改为 POST,您应该这样做:

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