IOS - 使用REST API进行基本身份验证 - SSL - AFNetworking 3.0

时间:2016-12-12 20:50:04

标签: ios afnetworking-3

一切都很好用代码,问题是它说认证失败了,虽然用户名和密码是100%正确的,所以不确定是否有办法传递登录和密码并获得用户认证

 NSString *urlString = @"URL";

        NSMutableArray * keyStrings = [NSMutableArray new];
        NSMutableArray * valueStrings = [NSMutableArray new];
        [keyStrings addObject:@"user"];
        [valueStrings addObject:@"abc"];
        [keyStrings addObject:@"password"];
        [valueStrings addObject:@"12345"];

        NSDictionary * requestDict = [[NSDictionary alloc] initWithObjects:valueStrings forKeys:keyStrings];
        AFHTTPSessionManager* manager = [AFHTTPSessionManager manager];

        manager.responseSerializer = [AFHTTPResponseSerializer serializer];

        manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/xml"];
        manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
        manager.securityPolicy.allowInvalidCertificates = YES;
        manager.securityPolicy.validatesDomainName = NO;

1 个答案:

答案 0 :(得分:1)

我猜你是否打算使用HTTP Basic Authentication,在这种情况下你应该使用Authorization标题字段,而不是在请求中以纯文本格式传递用户名和密码#39; s参数(我第一次尝试使用REST API进行基本身份验证时犯了这个错误。)

您需要将其添加到AFHTTPRequestSerializer的标题中,您可以使用setAuthorizationHeaderFieldWithUsername:password: method或手动构建标题字段值并使用以下方法设置标题字段值setValue:forHTTPHeaderField: method