试图将我的代码ASIHttpRequest
从AFNetworking
. 似乎已经提出了类似的问题,但couldnt
找到了解决我的问题的方法。
我的代码在ASIHttpRquest
.
我向我的服务器发送一个简单的 post 请求并监听 http 响应。如果http response
是 200 一切正常,但如果我发送另一个状态代码 >400AFNetworking
块失败。
服务器端响应:
$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
我怎样才能解决这个问题?