【问题标题】:Response failure when using AFHTTPRequestOperationManager使用 AFHTTPRequestOperationManager 时响应失败
【发布时间】:2023-03-14 19:50:01
【问题描述】:

当我尝试调用 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)}

【问题讨论】:

    标签: ios objective-c afnetworking afnetworking-2


    【解决方案1】:

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

    【讨论】:

    • 布兰登·罗斯:感谢您的回答。我收到了 responseString 的回复
    • Brandon Roth :当我在设备中运行时获得 ld 的另一个帮助:找不到 -lPods clang 的库:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)
    • lPods 问题是 cocoapods 的问题。谷歌一下,你应该能够找到解决方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多