Afnetworking 2 基本认证帖子

Posted

技术标签:

【中文标题】Afnetworking 2 基本认证帖子【英文标题】:Afnetworking 2 basic auth post 【发布时间】:2014-05-13 17:38:48 【问题描述】:

我正在尝试使用 Afnetworking 复制下一条卷曲线:

curl -basic -u "DD22137584:123456" -d "modulo=dnsDinamico&archivo=dnsDinamicoDesktop&op=obtenertHosts" "https://administracion.XXXXX.com/ws/api.php"

所以, 我创建了一个 AFHTTPSessionManager 的子类,按照不同的方式最终编码:

#import "DDDAPIClient.h"

NSString * const kBaseURLString = @"https://administracion.XXXXX.com/ws/api.php";

@interface DDDAPIClient()

@property (nonatomic, strong) NSDictionary *params;

@end


@implementation DDDAPIClient

-(NSDictionary *) params
if(!_params) _params = [[NSDictionary alloc] initWithObjects:@[@"dnsDinamico", @"dnsDinamicoDesktop", @"actualizarHosts"] forKeys:@[@"modulo", @"archivo", @"op"]];

return _params;



+ (DDDAPIClient *)sharedClient 
static DDDAPIClient *_sharedClient = nil;
static dispatch_once_t oncePredicate;
dispatch_once(&oncePredicate, ^
    _sharedClient = [[self alloc] initWithBaseURL:[NSURL URLWithString:kBaseURLString]];
);
return _sharedClient;



- (instancetype)initWithBaseURL:(NSURL *)url 

self = [super initWithBaseURL:url];
if (!self) 
    return nil;


return self;


- (void)getHostsFiltered:(NSString *)filter
             success:(void(^)(NSURLSessionDataTask *task, id responseObject))success
             failure:(void(^)(NSURLSessionDataTask *task, NSError *error))failure


[self.requestSerializer clearAuthorizationHeader];
[self.requestSerializer setAuthorizationHeaderFieldWithUsername:@"DD22137584" password:@"123456"];
[self setRequestSerializer:[AFHTTPRequestSerializer serializer]];


[self POST:kBaseURLString parameters:self.params success:^(NSURLSessionDataTask *task, id responseObject) 
    if (success) 

        success(task, responseObject);
    
 failure:^(NSURLSessionDataTask *task, NSError *error) 
    if (failure) 
        failure(task, error);
    
];


但是,现在,调用返回了下一个错误:

2014-05-13 14:45:36.281 DynamicDnsDW[5865:60b] Failure -- Error Domain=AFNetworkingErrorDomain Code=-1011 "Request failed: unauthorized (401)" UserInfo=0x8c59bf0 NSErrorFailingURLKey=https://administracion.XXXXX.com/ws/api.php, AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0x8c59ad0>  URL: https://administracion.donweb.com/ws/api.php   status code: 401, headers 
Connection = close;
"Content-Type" = "text/html";
Date = "Tue, 13 May 2014 17:45:36 GMT";
P3P = "CP=\"IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT\"";
Server = "Apache/2.2.3 (CentOS)";
"Transfer-Encoding" = Identity;
"Www-Authenticate" = "Basic realm=\"api.json\"";
"X-Powered-By" = "PHP/5.1.6";
 , NSLocalizedDescription=Request failed: unauthorized (401)

你能帮帮我吗?

谢谢

【问题讨论】:

【参考方案1】:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
NSDictionary *parameters = @@"Email": username, @"Passwd":password;
[manager POST:@"https://www.inoreader.com/accounts/ClientLogin" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) 
    NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
    NSString *auth = [[string split:@"Auth="] last];
    block(auth, nil);
 failure:^(AFHTTPRequestOperation *operation, NSError *error) 
    block(nil, error);
];

这对我有用。

【讨论】:

以上是关于Afnetworking 2 基本认证帖子的主要内容,如果未能解决你的问题,请参考以下文章

[AFN]AFNetworking错误总结

IOS AFN (第三方请求)

AFNetWorking 网络请求转载

AFN3.0封装

AFN2.0到3.0的迁移

iOS AFNetworking 3.0以上使用