Afnetworking 2基本授权帖子

时间:2014-05-13 17:38:49

标签: ios http-post basic-authentication afnetworking-2

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

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

所以, 我创建了一个AFHTTPSessionManager的类子项跟随不同的tuts到最后代码:

#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 个答案:

答案 0 :(得分: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);
}];

这适合我。