the*_*jaz 9 cocoa-touch objective-c nsurlconnection ios afnetworking
情况
如果它返回401,我重试如下:
AFHTTPRequestOperation *requestOperation = [MyHTTPClient.sharedClient HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) {
processSuccessBlock(operation, responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
processFailureBlock(operation, error);
if (operation.response.statusCode == 401)
[MyHTTPClient.sharedClient refreshTokenAndRetryOperation:operation success:processSuccessBlock failure:processFailureBlock];
}];
Run Code Online (Sandbox Code Playgroud)问题
我做了什么
cancelAuthenticationChallenge:于willSendRequestForAuthenticationChallenge:被中止第二个电话,但它给出了错误代码-1012(NSURLErrorUserCancelledAuthentication),而不是401和掩盖了真实的反应如何禁用身份验证质询响应机制,以便在不调用它的情况下获得服务器响应?
您有几个选项可以完全自定义身份验证处理:
利用setWillSendRequestForAuthenticationChallengeBlock:和setAuthenticationAgainstProtectionSpaceBlock: 从类中AFURLConnectionOperation设置相应的块,您可以在其中定制所需的机制.
标题包含文档.
覆盖connection:willSendRequestForAuthenticationChallenge:在子类中AFURLConnectionOperation.这将有效地覆盖AFNetworking中的完整身份验证机制设置.
请注意,您无法禁用"服务器验证客户端身份"身份验证质询 - 这是服务器的一部分.您必须为请求的身份验证方法提供正确的凭据.