NSMutableURLRequest请求

1.第一种方法example

-(BOOL)loginRequest

{

NSURL *url = [NSURL URLWithString[urlPath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];//是为了保证URL字符串有效

    NSString *bodyString = [NSString stringWithFormat:@"userNumber=%@&password=%@",_Ktext.text,_Ltext.text];//账号密码

    NSData *bodyData =[bodyString dataUsingEncoding:NSUTF8StringEncoding];//bodyString转换为NSData数据

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheDatatimeoutInterval:10];

    [requestsetHTTPMethod:@"POST"];

    [requestsetHTTPBody:bodyData];

//[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];


NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

    if (!connection) {

        NSLog(@"networkfail!");

        return NO;

    }

    else

    {

        NSLog(@"request succeed!");

        return YES;

    }

}


//实现NSURLConnectiondelegate

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data

{

    NSString *receiveStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

    if ([receiveStr isEqualToString:@"shibai"]|| [receiveStr isEqualToString:@"null"]) {

        NSLog(@"receStr:%@",receiveStr);

    }

    else { //数据返回成功则进行json解析,此为例子,不保证全部返回数据均为json格式

        NSDictionary *receiveDict = [NSJSONSerialization JSONObjectWithData:dataoptions:NSJSONReadingMutableLeaveserror:nil];

        showBook *showmybook = [[showBook alloc] initWithNibName:@"showBook" bundle:nil];

        showmybook.bookArr = [self jsonParse:receiveDict];

        showmybook.delegate = self;

        showmybook.modalTransitionStyle = UIModalTransitionStylePartialCurl;

        [self presentModalViewController:showmybook animated:YES];

        [showmybook release];

    }

    [receiveStr release];

}


2.第二种方法example

-(BOOL)loginRequest

{

NSURL *url = [NSURL URLWithString[urlPath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];//是为了保证URL字符串有效

    NSString *bodyString = [NSString stringWithFormat:@"userNumber=%@&password=%@",_Ktext.text,_Ltext.text];//账号密码

    NSData *bodyData =[bodyString dataUsingEncoding:NSUTF8StringEncoding];//bodyString转换为NSData数据

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheDatatimeoutInterval:10];

    [requestsetHTTPMethod:@"POST"];

    [requestsetHTTPBody:bodyData];


NSError *error  //发起一个同步请求或者异步请求,data即为返回数据

NSData *data =[NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];

 

    if (error) {

        NSLog(@"networkfail!");

        return NO;

    }

    else

    {

        NSLog(@"request succeed!");

NSString *receiveStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];


if ([receiveStr isEqualToString:@"shibai"]|| [receiveStr isEqualToString:@"null"]) 

{

       NSLog(@"receStr:%@",receiveStr);

    }

    else {//数据返回成功则进行json解析,此为例子,不保证全部返回数据均为json格式

        NSDictionary *receiveDict = [NSJSONSerialization JSONObjectWithData:dataoptions:NSJSONReadingMutableLeaveserror:nil];

        showBook *showmybook = [[showBook alloc] initWithNibName:@"showBook" bundle:nil];

        showmybook.bookArr = [self jsonParse:receiveDict];

        showmybook.delegate = self;

        showmybook.modalTransitionStyle = UIModalTransitionStylePartialCurl;

        [self presentModalViewController:showmybook animated:YES];

        [showmybook release];

   }

[receiveStr release];

        return YES;

    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值