使用AFHTTPRequestOperationManager时响应失败

时间:2014-07-25 05:20:54

标签: ios objective-c afnetworking afnetworking-2

当我尝试使用确切的用户名和密码调用API(AFHTTPRequestOperationManager)进行身份验证方法时获得准确的响应并且方法成功。然后我尝试使用无效的用户名和密码我得到的回复是失败。

我尝试了ASIHTTPRequest API,因为它的工作正常。所以请检查以下请求和回复,并建议我如何解决这个问题。但是在AFHTTPRequestOperationManager中无效的用户响应是失败的。

ASIHTTPRequest CALL示例:

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[ NSURL URLWithString:[SharedUtil URLByKey:@"A_URL"]]];
request.shouldAttemptPersistentConnection=NO;
[request setRequestMethod:@"POST"];
[request addRequestHeader:@"enctype" value:@"application/x-www-form-urlencoded"];
[request addRequestHeader:@"Authorization" value:[SharedUtil authorizationHeader ]];
[request addRequestHeader:@"Accept" value:@"application/json"];

[request setValidatesSecureCertificate:NO];

[request setPostValue:@"pa" forKey:@"type"];

[request setPostValue:username forKey:@"username"];

[request setPostValue:password forKey:@"password"];


[request setDelegate:self];

[request setTimeOutSeconds:120];

[self setTimer];
[request setDidFinishSelector: @selector(responseAuthUserSuccess:)];
[request setDidFailSelector: @selector(responseAuthUserFailed:)];
[post release];
// Cancels an asynchronous request

[networkQueue addOperation: request];

响应是ASIHTTPRequest,方法成功

{“error”:“invalid_grant”,“error_description”:“无法验证用户sdsdds@gmail.com”}

使用AFHTTPRequestOperationManager CALL示例:

AFHTTPRequestOperationManager *managerAF = [AFHTTPRequestOperationManager manager];

NSDictionary *params = @{
                             @"type": @"pa",
                             @"username": username,
                             @"password": password
                             };
    [managerAF.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [managerAF.requestSerializer setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"enctype"];
    [managerAF.requestSerializer setValue:[SharedUtil authorizationHeader] forHTTPHeaderField:@"Authorization"];

    managerAF.responseSerializer = [AFJSONResponseSerializer serializer];
    managerAF.securityPolicy.allowInvalidCertificates = YES;
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];



    [managerAF POST:[SharedUtil URLByKey:@"A_URL"] parameters:params  success:^(AFHTTPRequestOperation *operation, id responseObject) {


        NSData* data=[operation.responseString dataUsingEncoding:NSUTF8StringEncoding];


      }
        [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
        [self stopTimer];


    }];

AFHTTPRequestOperationManager响应和方法失败:

Printing description of error:
Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: bad request (400)" UserInfo=0xd3b7670 {NSErrorFailingURLKey=https://example.com/token, com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0xd29a010> { URL: https://example.com/token } { status code: 400, headers {
    "Cache-Control" = "no-store";
    Connection = "keep-alive";
    "Content-Type" = "application/json;charset=UTF-8";
    Date = "Fri, 25 Jul 2014 05:15:19 GMT";
    Pragma = "no-cache";
    Server = "nginx/1.6.0";
    "Transfer-Encoding" = Identity;
    "X-Powered-By" = "Servlet/3.0 JSP/2.2 (GlassFish Server Open Source Edition 3.1.2.2 Java/Oracle Corporation/1.7)";
} }, NSLocalizedDescription=Request failed: bad request (400)}

1 个答案:

答案 0 :(得分:8)

你击中你的失败阻止,因为api你的命中返回一个400的http响应代码,AFNetworking正确地将其解释为错误。通过使用故障块中提供的AFHTTPRequestOperation对象的responseDataresponseString属性,您仍然可以从响应中获取更详细的数据。