iOS中NSURLSession的使用

来源:互联网 发布:淘宝怎么装修手机店铺 编辑:程序博客网 时间:2024/05/17 01:58

快一年左右未在CSDN上发表新文章了,基本上都保存在自己的本地。难得闲下来,还是将自己的一些资料发布到网上供大家一起交流,参考。

        NSURLSession是苹果一种HTTP网络请求方式,想必有经验的都知道在iOS开发中,HTTP网络请求方式有很多种。比如:

ASIHTTPRequest

AFNetworking等

在以前的开发中,我比较习惯使用ASIHTTPRequest,但是自从ARC之后,ASIHTTPRequest没有更新升级了,不过还是能够满足日常的开发需求,同时使用cocospods管理,ASIHTTPRequest在ARC的条件下,也比较方便了。当然ASIHTTPRequest与AFNetworking之间各有优缺点。同时毕竟是第三方库存在太大不确定的因素,为此,我在日常的开发当中,慢慢的移除第三方库或自己基于苹果提供的API进行二次封装,提供接口。好了,不废话了,切入本文的主题,NSURLSession的使用,直接上代码:

1、简单请求

NSURLRequest *request = [[NSURLRequest allocinitWithURL:[NSURL URLWithString:@"http://www.baidu.com"]];

    NSURLSession *urlSession = [NSURLSession sharedSession];

    NSURLSessionDataTask *dataTask = [urlSession dataTaskWithRequest:request

                                                   completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

                                                       NSLog(@"data > %@", data);

                                                   }];

    [dataTask resume];

    //    [dataTask suspend];

    //    [dataTask cancel];

    

    NSURLSessionDownloadTask *downloadTask = [urlSession downloadTaskWithRequest:request

                                                               completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) {

                                                                   NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectoryNSUserDomainMaskYESfirstObject];

                                                                   NSURL *documentsDirectoryURL = [NSURL fileURLWithPath:documentsPath];

                                                                   NSURL *newFileLocation = [documentsDirectoryURL URLByAppendingPathComponent:[[response URLlastPathComponent]];

                                                                   [[NSFileManager defaultManagercopyItemAtURL:location toURL:newFileLocation error:nil];

                                                               }];

    [downloadTask resume];

    

    NSURLSessionUploadTask *uploadTask = [urlSession uploadTaskWithRequest:request

                                                                  fromData:nil

                                                         completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

                                                             

                                                         }];

    [uploadTask resume];


2、POST请求

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];

    

    NSMutableURLRequest *request = [[NSMutableURLRequest allocinitWithURL:[NSURL URLWithString:@"url地址"]];

    [request setHTTPMethod:@"POST"];

    [request setHTTPBody:[@"version=1.0.0&os_version=7.1.2&os_type=iOS" dataUsingEncoding:NSUTF8StringEncoding]];

    NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration];

    NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request

                                                completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

                                                    

                                                    NSLog(@"获取到的数据>%@", [[NSString allocinitWithData:data

                                                                                              encoding:NSUTF8StringEncoding]);

                                                }];

    [dataTask resume];


0 0
原创粉丝点击