码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-01-06 22:49:45      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:

#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=[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请求,向服务器发送用户信息

标签:

原文地址:http://www.cnblogs.com/xiangli/p/4207054.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!