码迷,mamicode.com
首页 > Web开发 > 详细

NSURLSession的用法

时间:2016-11-05 20:06:22      阅读:253      评论:0      收藏:0      [点我收藏+]

标签:mutable   hand   用法   shared   using   name   leave   void   task   

 

-(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的用法

标签:mutable   hand   用法   shared   using   name   leave   void   task   

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

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