首页 > Web开发 > 详细

NSURLSession的用法

时间:2016-11-05 20:04:10      阅读:278      评论:0      收藏:0      [点我收藏+]

 

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

  

    //[self getTest];

    [self postTest];

}

 

-(void) getTest{

    NSURLSession *session = [NSURLSession sharedSession];

    NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/video?type=JSON"];

    

    NSURLSessionTask *task = [session dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

        NSLog(@"%d",data.length);

        

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

        NSLog(@"%@",dict);

    }];

    [task resume];

}

-(void) postTest{

    NSURLSession *session = [NSURLSession sharedSession];

    //url = http://120.25.226.186:32812/login?username=123&pwd=123&type=JSON

    NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/login"];

    

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

    

    request.HTTPMethod=@"POST";

    request.HTTPBody = [@"username=123&pwd=123&type=JSON" dataUsingEncoding:NSUTF8StringEncoding];

 

    NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

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

        NSLog(@"___%@",dict);

    }];

    [task resume];

 

 

}

NSURLSession的用法

原文:http://www.cnblogs.com/seeworld/p/6033561.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!