AFNetworking预期状态代码(200-299),得到403

时间:2013-05-30 20:08:19

标签: ios objective-c post afnetworking

尝试将我的代码从ASIHttpRequest迁移到AFNetworking。似乎已经提出了类似的问题,但couldnt找到了我的问题的解决方案。

我的代码与ASIHttpRquest一起工作正常。

我向我的服务器发送一个简单的帖子请求并听取http响应。如果http response为200,则一切正常但如果我发送另一个状态代码> 400 AFNetworking阻止失败。

服务器端响应:

$rc = $stmt->fetch();
    if ( !$rc ) {
    //  echo "no such record\n";
      $isrecordExist=0; //false does not exists
      sendResponse(403, 'Login Failed');
      return false;
    }
    else {
     // echo 'result: ', $result, "\n";
       $sendarray = array(
            "user_id" => $result,
        );
        sendResponse(200, json_encode($sendarray));
    }

IOS部分:

AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:
                            [NSURL URLWithString:server]];
    client.allowsInvalidSSLCertificate=YES;

    [client postPath:loginForSavingCredientials parameters:params success:^(AFHTTPRequestOperation *operation, id response) {
    if (operation.response.statusCode == 500) {}
     else if (operation.response.statusCode == 403) {}
     else if (operation.response.statusCode == 200) {//able to get results here            NSError* error;
        NSString *responseString = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
        NSDictionary* json =     [NSJSONSerialization JSONObjectWithData: [responseString dataUsingEncoding:NSUTF8StringEncoding]
                                                                 options: NSJSONReadingMutableContainers
                                                                   error: &error];}
 } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"failure %@", [error localizedDescription]);
    }];

的NSLog:

 failure Expected status code in (200-299), got 403

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:12)

AFNetworking获得2xx(成功)状态代码时,它会调用成功块。

当它收到4xx(客户端错误)或5xx(服务器错误)状态代码时,它会因为出错而调用失败块。

所以你需要做的就是将检查500或403状态代码移到故障区。

AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:server]];
client.allowsInvalidSSLCertificate=YES;

[client postPath:loginForSavingCredientials parameters:params success:^(AFHTTPRequestOperation *operation, id response) {
    if (operation.response.statusCode == 200) {//able to get results here            NSError* error;
        NSString *responseString = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
        NSDictionary* json = [NSJSONSerialization JSONObjectWithData: [responseString dataUsingEncoding:NSUTF8StringEncoding]
                                                             options: NSJSONReadingMutableContainers
                                                               error: &error];
    }
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"failure %@", [error localizedDescription]);
    if (operation.response.statusCode == 500) {}
    else if (operation.response.statusCode == 403) {}
}];

答案 1 :(得分:1)

创建请求操作时,您需要告诉它哪些响应状态代码是可接受的(平均成功)。默认情况下,这是200 - >范围内的代码。 299。

开始使用客户端前的设置:

AFHTTPRequestOperation.acceptableStatusCodes = ...;

[client postPath:

文档为here