首页 > 代码库 > Post请求,向服务器发送用户信息

Post请求,向服务器发送用户信息

#define kRegist @"http://api.sucar.com.cn/mobile/index.php?app=mobile&controller=member&action=register"

#define kland @"http://api.sucar.com.cn/mobile/index.php?app=mobile&controller=member&action=login"

@property(nonatomic,strong)UITextField *userName;

@property(nonatomic,strong)UITextField *passWord;

@property(nonatomic,strong)UITextField *email;

-(void)regist

{

    NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:kRegist]];

 //设置请求类型

    [request setHTTPMethod:@"post"];

    NSString * body=[NSString stringWithFormat:@"username=%@&password=%@&email=%@",self.userName.text,self.passWord.text,self.email.text];

    NSData *data=http://www.mamicode.com/[body dataUsingEncoding:NSUTF8StringEncoding];

//设置包体

    [request setHTTPBody:data];

    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {

        NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];

        NSString *result=dict[@"message"];

        NSString *s=dict[@"state"];

        NSInteger state=[s integerValue];

        if (state==0) {

           UIAlertView *wrong=[[UIAlertView alloc] initWithTitle:@"警告" message:result delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];

            [wrong show];

        }else if(state==1)

        {

            UIAlertView *right =[[UIAlertView alloc] initWithTitle:nil message:@"注册成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];

            [right show];

        }

    }];

}

Post请求,向服务器发送用户信息