NSURLSessionBaseDemo

@interface ViewController ()<NSURLSessionDataDelegate>

//存放请求返回的数据

@property (nonatomic,strong) NSMutableData *resultData;

@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    //

//    NSURLSession

    //1 NSURL

    NSURL *url = [NSURL URLWithString:@"http://10.0.8.8/sns/my/user_list.php"];

    // 2(get)NSURLRequest

//    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    // 2(post) NSMutableURLRequest

    NSMutableURLRequest *request =[NSMutableURLRequest requestWithURL:url ];

    request.HTTPMethod = @"post";

    //post 传参数

    request.HTTPBody = [@"number=10&page=2" dataUsingEncoding:NSUTF8StringEncoding];

    

    

    //首先需要一个工作模式 1.普通的任务(get post2.瞬时任务 3.后台任务

    //3 工作模式 默认工作模式

    NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];

    //请求的超时时间

    config.timeoutIntervalForRequest = 60;

    

    //4 创建NSURLSession

    NSURLSession *session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:[NSOperationQueue mainQueue]];

    //5 下载任务task

    NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request];

    //6 开始任务

    [dataTask resume];

}


#pragma mark - NSURLSessionDataDelegae


- (void)URLSession:(NSURLSession *)session dataTask:(nonnull NSURLSessionDataTask *)dataTask didReceiveResponse:(nonnull NSURLResponse *)response completionHandler:(nonnull void (^)(NSURLSessionResponseDisposition))completionHandler {

    //

    NSLog(@"请求已经开始了,收到了回应 %@",response);

//     "Content-Type" = "text/html"; 返回的内容类型

//     "Content-Length" = 750; 内容的长度,将要下载的数据有多大

//    status code: 200, 状态码

//    URL: http://10.0.8.8/sns/my/user_list.php 我们请求的接口

    //所有响应头内容(response响应)

    NSDictionary *responseHeader = [(NSHTTPURLResponse*)response allHeaderFields];

    NSLog(@"%@",responseHeader[@"Content-Length"]);

    

    //初始化 存放数据的data

    _resultData = [[NSMutableData alloc]init];

    

    //这个block 一定要调用

    completionHandler(NSURLSessionResponseAllow);


}

// 接受数据

- (void)URLSession:(NSURLSession *)session dataTask:(nonnull NSURLSessionDataTask *)dataTask didReceiveData:(nonnull NSData *)data {

    NSLog(@"接受到数据 %ld",data.length);

    //每次接受到数据的时候,都添加resultdata

    [self.resultData appendData:data];

}


- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {

    NSLog(@"请求结束了");

    if (error) {

        NSLog(@"请求失败了 %@",error);

    }else  {

        NSLog(@"请求成功了");

        //json 解析

        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:self.resultData options:NSJSONReadingMutableContainers error:nil];

        NSLog(@"%@",dict);

    }

}



你可能感兴趣的:(NSURLSessionBaseDemo)