試圖將我的代碼從ASIHttpRequest
遷移到AFNetworking
。似乎有類似的問題,但couldnt
找到解決我的問題。AFNetworking在(200-299)中的預期狀態碼,得到403
我的代碼與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
我該如何解決這個問題?