POST提交数据

//post提交的参数,格式如下:  
    //参数1名字=参数1数据&参数2名字=参数2数据&参数3名字=参数3数据&...  
    NSString *post = [NSString stringWithFormat:@"m=check_user_login_info&f=itemgarden_iphone_app&u=%@&s=%@",self.us_email,self.us_password];  
      
    NSLog(@"post:%@",post);  
      
    //将NSSrring格式的参数转换格式为NSData,POST提交必须用NSData数据。  
    NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];  
    //计算POST提交数据的长度  
    NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]];  
    NSLog(@"postLength=%@",postLength);  
    //定义NSMutableURLRequest  
    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];  
    //设置提交目的url  
    [request setURL:[NSURL URLWithString:kUserLoginCheckUrl]];  
    //设置提交方式为 POST  
    [request setHTTPMethod:@"POST"];  
    //设置http-header:Content-Type  
    //这里设置为 application/x-www-form-urlencoded ,如果设置为其它的,比如text/html;charset=utf-8,或者 text/html 等,都会出错。不知道什么原因。  
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];  
    //设置http-header:Content-Length  
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];  
    //设置需要post提交的内容  
    [request setHTTPBody:postData];  
      
    //定义  
    NSHTTPURLResponse* urlResponse = nil;  
    NSError *error = [[NSError alloc] init];  
    //同步提交:POST提交并等待返回值(同步),返回值是NSData类型。  
    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];  
    //将NSData类型的返回值转换成NSString类型  
    NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];  
    NSLog(@"user login check result:%@",result);  
      
    if ([@"success" compare:result]==NSOrderedSame) {  
        return YES;  
    }  
    return NO;
转自:http://stephen830.iteye.com/blog/1703086
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值