使用AFHTTPRequestOperationManager时响应失败 [英] Response failure when using AFHTTPRequestOperationManager

查看:159
本文介绍了使用AFHTTPRequestOperationManager时响应失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

When i tried to call API(AFHTTPRequestOperationManager) for authentication method with exact username and password getting exact response and method was success. Then i tried with invalid username and password I'm getting response is failure.

我尝试了ASIHTTPRequest API,因为同样的方法也能正常工作.因此,请检查下面的请求和响应,并向我提出如何解决此问题的建议.但是在AFHTTPRequestOperationManager中,无效的用户响应是失败的.

i tried ASIHTTPRequest API for same method its working fine. So please check below request and response and advice me how to resolve this issues. But in AFHTTPRequestOperationManager invalid user response is failure.

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,方法是成功

{错误":"invalid_grant",错误说明":无法验证用户sdsdds@gmail.com"}

{"error":"invalid_grant","error_description":"Unable to validate user 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)}

推荐答案

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

Your hitting your failure block because the api your hitting is returning an http response code of 400 and AFNetworking is correctly interpreting that as an error. You can still get the more detailed data from the response by using the responseData and responseString properties of the AFHTTPRequestOperation object provided in the failure block.

这篇关于使用AFHTTPRequestOperationManager时响应失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆