ios - AFNetworking 更改重定向请求从 GET 到 POST

标签 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 做同样的事情吗?

最佳答案

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

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

关于ios - AFNetworking 更改重定向请求从 GET 到 POST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29131556/

相关文章:

ios - 如何在 UITableView 上设置 VoiceOver 初始焦点

xcode - Xcode 中的 Storyboard太慢了

tomcat - 使用tomcat将请求和 header 转发到不同的主机

javascript - Node.js 重定向到 ip :port based on the used domain name

ios - 从 iOS 发送 JSON,奇怪的响应

iphone - NSJSONSerialization 空白 uitableview 和 null 结果

iphone - 输入大文本时文本字段的大小会发生变化

ios - 编译 xcode 项目时 Alamofire 出现大量问题

objective-c - 如何将此 subview 推送到它自己的子类中?

apache - 将 URL 重定向到另一个 URL