Response failure when using AFHTTPRequestOperationManager

ぃ、小莉子 提交于 2019-12-01 07:51:30

问题


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.

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 sample:

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

Response is ASIHTTPRequest and method is success

{"error":"invalid_grant","error_description":"Unable to validate user sdsdds@gmail.com"}

Using AFHTTPRequestOperationManager CALL sample:

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 Response and method is failure :

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:


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.



来源:https://stackoverflow.com/questions/24948561/response-failure-when-using-afhttprequestoperationmanager

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!