【问题标题】:AFNetworking change redirect request from GET to POSTAFNetworking 将重定向请求从 GET 更改为 POST
【发布时间】:2015-03-18 19:58:49
【问题描述】:

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

【问题讨论】:

    标签: ios xcode redirect afnetworking afnetworking-2


    【解决方案1】:

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

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

    【讨论】:

    • 别担心,这种事经常发生在我身上!
    猜你喜欢
    • 1970-01-01
    • 2019-02-01
    • 2018-01-10
    • 2019-10-12
    • 2019-10-04
    • 1970-01-01
    • 2021-05-11
    • 2021-11-01
    • 2021-01-16
    相关资源
    最近更新 更多