AFNetworking 3.0 使用 AFHTTPSessionManager 如何获得 http 的响应状态码
AFHTTPSessionManager status code AFNetworking 3.0
在获取的 task时 的 task.response 类型为NSURLResponse此类不包含http的回复状态码 需要向上类型转换将其转为他的子类 NSHTTPURLResponse,NSHTTPURLResponse包含状态码。
[manager POST:User_login_url parameters:dic success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSError *errorForJSON = [NSError errorWithDomain:@"请求数据解析为json格式,发出错误" code:2014 userInfo:@{@"请求数据json解析错误": @"中文",@"serial the data to json error":@"English"}];
NSLog(@"responseObject is %@", responseObject);
NSLog(@"the responseObject class is %@", [responseObject class]);
NSDictionary *responsedic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
NSHTTPURLResponse *urlResponse = (NSHTTPURLResponse *)task.response;
NSLog(@"the response state code is %ld", (long)urlResponse.statusCode);
NSLog(@"the responseDic is %@", responsedic);
// task.response
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"the task is %@", task);
NSHTTPURLResponse *urlResponse = (NSHTTPURLResponse *)task.response;
NSLog(@"the response state code is %ld", (long)urlResponse.statusCode);
NSLog(@"error is %@", error.description);
}];
}];