我们可以使用第三框架AFNetworking,用它的post方法进行与服务器进行通信。若服务器返回的userId为10则成功,返回的为11则表示用户名或者密码错误。
<span style="font-size:14px;">-(void)login:(id)sender{
AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
NSString *url=@"http://172.18.144.248:8080/upload_file_service/LoginServlet";
manager.responseSerializer.acceptableContentTypes=[NSSet setWithObjects:@"text/html",nil];
NSDictionary *namepwd=@{@"user":@"admin",@"pass":@"123"};
[manager POST:url parameters:namepwd success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"post-->%@",responseObject);
NSDictionary *datare=[[NSDictionary alloc] init];
datare=responseObject;
re=[datare objectForKey:@"userId"];
appStatus *transvalue=[appStatus shareInstance];
mainViewController *mainView=[self.storyboard instantiateViewControllerWithIdentifier:@"second"];
mainView.delegate=self;
transvalue.contextStr=__name.text;
NSNumber *fre=[[NSNumber alloc]initWithInt:10];
BOOL flag=[fre isEqualToNumber:re];
if(flag){
[self presentViewController:mainView animated:YES completion:nil];
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"%@",@”用户名或者密码错误”);
}];
}</span>