2016-10-21 56 views
1

我正在使用新的afnetworking 3.0庫使網絡請求使用AFHTTPSessionmanager進行登錄。所以當我做一個POST調用時,如果我得到401,在響應中,有另一個網絡請求被默默地調用。AFNetworking AFHTTPSessionManager POST調用兩次

如何通過在失敗回調中再次調用服務來避免它?

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

回答

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; 
}]; 

這一切都取決於您如何驗證Web服務上的用戶。

+0

搶你是救世主......非常感謝......我正在返回NSURLSessionAuthChallengeUseCredential,它正在悄悄地發出另一個請求......但在更改爲行後返回NSURLSessionAuthChallengeCancelAuthenticationChallenge它工作得很好.... – lifemoveson