AFNetworking(AFHTTPSessionManager)之http摘要认证(DIGEST)

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

[manager setTaskDidReceiveAuthenticationChallengeBlock:^ NSURLSessionAuthChallengeDisposition(NSURLSession * _Nonnull session, NSURLSessionTask * _Nonnull task, NSURLAuthenticationChallenge * _Nonnull challenge, NSURLCredential *__autoreleasing  _Nullable * _Nullable credential)

{

if (challenge.previousFailureCount == 0) {

NSURLCredential *myCredential = [[NSURLCredential alloc]initWithUser:用户名 password:密码  persistence:NSURLCredentialPersistenceForSession];

*credential = myCredential;

return NSURLSessionAuthChallengeUseCredential;

}  else {

return NSURLSessionAuthChallengeCancelAuthenticationChallenge;

}

}];

id successBlock = ^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject){

// 请求成功

};

id failureBlock = ^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error){

// 请求失败

};

[manager GET:url parameters:params success:successBlock failure:failureBlock];

你可能感兴趣的:(AFNetworking(AFHTTPSessionManager)之http摘要认证(DIGEST))