iOS 网络请求简述

iOS常用网络请求

1.NSURLConnection
2.NSURLSession
3.AFNetworking

NSURLConnection在iOS9被宣布弃用,
NSURLSession是苹果在iOS7后为HTTP数据传输提供的一系列接口,
AFNetworking是一个为 iOS 和 Mac OSX 制作的令人愉快的网络库。
当前使用 AFNetworking 3.0 版本
AFNetworking 下载地址 https://github.com/AFNetworking/AFNetworking

NSURLConnection

get异步请求

    // 请求路径
    NSString *urlString = @"http://www.weather.com.cn/data/sk/101010100.html?city=nanjing";
    // 创建URL
    NSURL *url = [NSURL URLWithString:urlString];
    // 创建请求
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    // 设置请求方法(默认就是GET请求,可以不设置)
    request.HTTPMethod = @"GET";
    // 发送异步请求
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
       
        NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        
    }];

post异步请求

    // 请求路径
    NSString *urlString = @"http://www.weather.com.cn/data/sk/101010100.html";
    // 创建URL
    NSURL *url = [NSURL URLWithString:urlString];
    // 创建请求
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    // 设置请求方法
    request.HTTPMethod = @"POST";
    // 设置请求体
    request.HTTPBody = [@"city=南京&pwd=123" dataUsingEncoding:NSUTF8StringEncoding];
    // 发送异步请求
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
        
        
        NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        
    }];

NSURLSession

get 请求

    // 快捷方式获得session对象
    // block 在子线程
    //    NSURLSession *session = [NSURLSession sharedSession];
   
     // block 在主线程
    NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:nil delegateQueue:[NSOperationQueue mainQueue]];
    
    NSURL *url = [NSURL URLWithString:@"http://www.weather.com.cn/data/sk/101010100.html?username=daka&pwd=123"];
    // 通过URL初始化task,在block内部可以直接对返回的数据进行处理
    NSURLSessionTask *task = [session dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError* error) {
        NSLog(@"%@", [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]);
    }];
    
    // 启动任务
    [task resume];

post 请求

    NSURL *url = [NSURL URLWithString:@"http://www.weather.com.cn/data/sk/101010100.html"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    
    request.HTTPMethod = @"POST";
    request.HTTPBody = [@"username=daka&pwd=123" dataUsingEncoding:NSUTF8StringEncoding];
    
    NSURLSession *session = [NSURLSession sharedSession];
    // 由于要先对request先行处理,我们通过request初始化task
    NSURLSessionTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        
        NSLog(@"%@", [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]);
                                        
    }];
    
    
    [task resume];

AFNetworking

AFNetworking 2.0以上兼容 NSURLSession
AFNetworking 3.0 get 请求

    NSString * urlString = @"http://www.weather.com.cn/data/sk/101010100.html";
    
    NSDictionary *parameters = @{@"username":@"daka",
                                 @"pwd":@"123"};
    
    //1、创建管理者对象
    AFHTTPSessionManager * manager = [NetWorkManager shareSessionManager];
    
    [manager GET:urlString parameters:parameters progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSDictionary * responseDict = responseObject;
        NSLog(@"%@",responseDict);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
    }];

AFNetworking 3.0 post 请求

    NSString * urlString = @"http://www.weather.com.cn/data/sk/101010100.html";
    
    NSDictionary *parameters = @{@"username":@"daka",
                                 @"pwd":@"123"};
    
    //1、创建管理者对象
    AFHTTPSessionManager * manager = [NetWorkManager shareSessionManager];
    
    [manager POST:urlString parameters:parameters progress:^(NSProgress *          _Nonnull uploadProgress) { 

     } 
    success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable   responseObject) {
        
        NSDictionary * responseDict = responseObject;
        NSLog(@"%@",responseDict);
  } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    
 }];

你可能感兴趣的:(iOS 网络请求简述)