我正在尝试使用Afnetworking复制下一个curl行:

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

所以,
我创建了AFHTTPSessionManager的Class子级,然后按照不同的方法进行最终的编码:
#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);
    }
}];

}

但是,现在, call 返回了下一个错误:
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)}

你能帮助我吗?

谢谢

最佳答案

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);
}];

这对我有用。

10-08 15:49