【问题标题】:AFNetworking AFHTTPSessionManager POST called twiceAFNetworking AFHTTPSessionManager POST 调用了两次
【发布时间】:2017-03-04 07:18:01
【问题描述】:

我正在使用新的 afnetworking 3.0 库发出网络请求以使用 AFHTTPSessionmanager 登录。因此,当我进行 POST 调用并在响应中收到 401 时,会静默调用另一个网络请求。

如何通过在失败回调中不再调用服务来避免它?

[manager POST:[[urlReq URL] absoluteString] parameters:nil progress:nil success:^(NSURLSessionTask *operation, id responseObject)

【问题讨论】:

    标签: ios objective-c afnetworking afnetworking-3


    【解决方案1】:

    如果您收到身份验证质询,您将看到第二个请求。

    如果需要,您可以直接拒绝授权质询,这会停止第二个请求:

    [manager setTaskDidReceiveAuthenticationChallengeBlock:^NSURLSessionAuthChallengeDisposition(NSURLSession * _Nonnull session, NSURLSessionTask * _Nonnull task, NSURLAuthenticationChallenge * _Nonnull challenge, NSURLCredential *__autoreleasing  _Nullable * _Nullable credential) {
        return NSURLSessionAuthChallengeCancelAuthenticationChallenge;
    }];
    

    或者,如果您想继续进行身份验证,您可以执行以下操作:

    [manager setTaskDidReceiveAuthenticationChallengeBlock:^NSURLSessionAuthChallengeDisposition(NSURLSession * _Nonnull session, NSURLSessionTask * _Nonnull task, NSURLAuthenticationChallenge * _Nonnull challenge, NSURLCredential *__autoreleasing  _Nullable * _Nullable credential) {
        if (challenge.previousFailureCount == 0) {
            *credential = [NSURLCredential credentialWithUser:self.user password:self.password persistence:NSURLCredentialPersistenceForSession];
            return NSURLSessionAuthChallengeUseCredential;
        }
    
        return NSURLSessionAuthChallengeCancelAuthenticationChallenge;
    }];
    

    这完全取决于您如何在您的网络服务上对用户进行身份验证。

    【讨论】:

    • Rob 你是救世主......非常感谢......我正在返回 NSURLSessionAuthChallengeUseCredential ,它正在默默地发出另一个请求......但是在更改为返回 NSURLSessionAuthChallengeCancelAuthenticationChallenge 行后,它工作得很好...... .
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-17
    • 1970-01-01
    • 2016-03-19
    • 2014-08-02
    • 1970-01-01
    • 1970-01-01
    • 2017-06-25
    相关资源
    最近更新 更多