NSURLSessionDataTask

来源:互联网 发布:oracle 自动优化 工具 编辑:程序博客网 时间:2024/06/17 08:59
 599人阅读 评论(1) 收藏 举报
(1)http://blog.csdn.net/rongxinhua/article/details/20079607?utm_source=tuicool&utm_medium=referral
(2)http://blog.csdn.net/majiakun1/article/details/38133703
//网址

http://blog.csdn.NET/lvchaman/article/details/50296921


开发iOS应用要调用Http接口、获取Http资源,有一套比较成熟的框架ASIHTTPRequest。而我还是比较喜欢使用原始一点的API,而它跟其他的面向对象语言有许多共通之处。本文分同步请求和异步请求这两种情况来讲解一下Http API的使用。直接上代码,注释即文档!

同步请求:即发起Http请求、获取并处理返回值都在同一个线程中进行

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. //创建URL对象  
  2. NSString *urlStr = @"http://blog.csdn.net/rongxinhua";  
  3. NSURL *url = [[NSURL alloc] initWithString:urlStr];  
  4.   
  5. //创建HTTP请求  
  6. //方法1(注:NSURLRequest只支持Get请求,NSMutableURLRequest可支持Get和Post请求)  
  7. NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];  
  8. NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];  
  9. //方法2,使用工厂方法创建  
  10. NSURLRequest *request = [NSURLRequest requestWithURL:url];  
  11. NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];  
  12. //同时设置缓存策略和超时时间  
  13. NSMutableURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:15];  
  14.   
  15. //设置Http头  
  16. NSDictionary *headers = [request allHTTPHeaderFields];  
  17. [headers setValue:@"iOS-Client-ABC" forKey:@"User-Agent"];  
  18.   
  19. //设置请求方法  
  20. [request setHTTPMethod:@"GET"];  
  21. [request setHTTPMethod:@"POST"];  
  22.   
  23. //设置要发送的正文内容(适用于Post请求)  
  24. NSString *content = @"username=stanyung&password=123";  
  25. NSData *data = [content dataUsingEncoding:NSUTF8StringEncoding];  
  26. [request setHTTPBody:data];  
  27.   
  28. //同步执行Http请求,获取返回数据  
  29. NSURLResponse *response;  
  30. NSError *error;  
  31. NSData *result = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];  
  32.   
  33. //返数据转成字符串  
  34. NSString *html = [[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding];  
  35.   
  36. //(如果有错误)错误描述  
  37. NSString *errorDesc = [error localizedDescription];  
  38.   
  39. //获取状态码和HTTP响应头信息  
  40. NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;  
  41. NSInteger statusCode = [httpResponse statusCode];  
  42. NSDictionary *responseHeaders = [httpResponse allHeaderFields];  
  43. NSString *cookie = [responseHeaders valueForKey:@"Set-Cookie"];  

注:以上代码,不要Copy直接执行,只是列举Http常用方法的调用。


异步请求:发起HTTP请求在一个线程中,返回结果处理在另一个线程中。相比同步请求,异步请求不需要等待返回结果,当前程序可以继续往下执行。在Objective-C中,异步请求也有两种实现方式:一种是注册回调代理,一种是使用回调代码块。

a.注册回调代理的方式:

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. [NSURLConnection connectionWithRequest:request delegate:self];  


需要实现NSURLConnectionDataDelegate协议:

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. @interface HttpDownloadService : NSObject<NSURLConnectionDataDelegate> {     
  2. }  


实现相关的协议方法:
[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. NSMutableData *buff;    //暂存响应的数据  
  2. bool finished = false;  //读完完整标记  
  3.   
  4. //收到HTTP响应时调用  
  5. -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {  
  6.     NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;  
  7.     NSDictionary *headers = [httpResponse allHeaderFields];  
  8.     buff = [[NSMutableData alloc] init];  
  9. }  
  10.   
  11. //读取返回数据时调用(可能会执行多次此方法)  
  12. -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {  
  13.     [buff appendData:data];  
  14. }  
  15.   
  16. //读完数据完成时调用  
  17. -(void)connectionDidFinishLoading:(NSURLConnection *)connection {  
  18.     NSString *html = [[NSString alloc] initWithData:buff encoding:NSUTF8StringEncoding];  
  19.     finished = true;  
  20. }  

通常情况下,数据在网络中传输,会受到带宽等因素的影响,并不会一次情将所有数据返回,你可能分几次才能接受完整一个HTTP响应报文。因此,(void)connection:(NSURLConnection *)didReceiveData:(NSData *) 这个方法很可能会执行多次。

上例代码中,使用了NSMutableData来暂存接收到的响应数据片段,每一段并接起来,直到读取完整。


b.使用回调代码块的方式:

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:  
  2.     ^(NSURLResponse *response, NSData *result, NSError *error){       //只会进入一次,方法内部已经实现了Buffer作用  
  3.         NSString *html = [[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding];  
  4.     }];  
跟NSURLConnectionDataDelegate的回调方法不同,这里的回调代码块只调用一次,它内部已经实现了Buffer的作用,等到数据接收完整后才进入此代码块。因此,你在代码块中获取到的result可以直接使用。


备注1:本文的代码例子将使用ARC编码模式,故所新建的对象均没有显式调用release()方法回收。

备注2:若你测试本例子代码新建的是Command Line Tool工程,在main函数中执行相关代码,上面两种异步执行的情况,你很可能你的程序没有执行到回调方法或回调代码块里面去,这是因为:在main函数中,主线程没有等待阻塞,一下子执行完了,回调代码所在的子线程可能未执行完或者根本还没开始执行,就已经因为主线程的结束而结束了。解决这个问题,可以在调用完异步方法后面,加以下代码:

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. while (!finished) {  
  2.     [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];  
  3. }  
而finished变量正是上面两个异步HTTP例子中定义是否执行完成的变量。

=======================

Session Task分为三种Data Task,Upload Task,Download Task。毫无疑问,Session Task是整个NSURLSession架构的核心目标。

下面写了一个简单的Demo来初步使用下三种任务对象。这里使用的是convenience methods,并没有定制session和使用协议,都是采用completionHandler作为回调动作。


故事板内容为:



第一种Data Task用于加载数据,使用全局的shared session和dataTaskWithRequest:completionHandler:方法创建。代码如下:

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. /* 使用NSURLSessionDataTask加载网页数据 */  
  2. - (IBAction)loadData:(id)sender {  
  3.     // 开始加载数据,让spinner转起来  
  4.     [self.spinner startAnimating];  
  5.       
  6.     // 创建Data Task,用于打开我的csdn blog主页  
  7.     NSURL *url = [NSURL URLWithString:@"http://blog.csdn.net/u010962810"];  
  8.     NSURLRequest *request = [NSURLRequest requestWithURL:url];  
  9.     NSURLSession *session = [NSURLSession sharedSession];  
  10.     NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request  
  11.                                                 completionHandler:  
  12.                                       ^(NSData *data, NSURLResponse *response, NSError *error) {  
  13.                                           // 输出返回的状态码,请求成功的话为200  
  14.                                           [self showResponseCode:response];  
  15.                                             
  16.                                           // 在webView中加载数据  
  17.                                           [self.webView loadData:data  
  18.                                                         MIMEType:@"text/html"  
  19.                                                 textEncodingName:@"utf-8"  
  20.                                                          baseURL:nil];  
  21.                                             
  22.                                           // 加载数据完毕,停止spinner  
  23.                                           [self.spinner stopAnimating];  
  24.                                       }];  
  25.     // 使用resume方法启动任务  
  26.     [dataTask resume];  
  27. }  
  28.   
  29. /* 输出http响应的状态码 */  
  30. - (void)showResponseCode:(NSURLResponse *)response {  
  31.     NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;  
  32.     NSInteger responseStatusCode = [httpResponse statusCode];  
  33.     NSLog(@"%d", responseStatusCode);  
  34. }  

completionHandler指定任务完成后的动作。注意一定要使用resume方法启动任务。(Upload Task和Download Task同理)

运行结果:



第二种Upload Task用于完成上传文件任务,使用方法类似:

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. /* 使用NSURLSessionUploadTask上传文件 */  
  2. - (IBAction)uploadFile:(id)sender {  
  3. //    NSURL *URL = [NSURL URLWithString:@"http://example.com/upload"];  
  4. //    NSURLRequest *request = [NSURLRequest requestWithURL:URL];  
  5. //    NSData *data = ...;  
  6. //      
  7. //    NSURLSession *session = [NSURLSession sharedSession];  
  8. //    NSURLSessionUploadTask *uploadTask = [session uploadTaskWithRequest:request  
  9. //                                                               fromData:data  
  10. //                                                      completionHandler:  
  11. //                                          ^(NSData *data, NSURLResponse *response, NSError *error) {  
  12. //                                              // ...  
  13. //                                          }];  
  14. //      
  15. //    [uploadTask resume];  
  16. }  


第三种Download Task用于完成下载文件的任务,使用全局的shared session和downloadTaskWithRequest:completionHandler:方法创建。

注意:在下载任务完成后,下载的文件位于tmp目录下,由代码块中的location指定(不妨输出看看),我们必须要在completion handler中将文件放到持久化的目录下保存。代码如下:

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. /* 使用NSURLSessionDownloadTask下载文件 */  
  2. - (IBAction)downloadFile:(id)sender {  
  3.     [self.spinner startAnimating];  
  4.       
  5.     NSURL *URL = [NSURL URLWithString:@"http://b.hiphotos.baidu.com/image/w%3D2048/sign=6be5fc5f718da9774e2f812b8469f919/8b13632762d0f703b0faaab00afa513d2697c515.jpg"];  
  6.     NSURLRequest *request = [NSURLRequest requestWithURL:URL];  
  7.     NSURLSession *session = [NSURLSession sharedSession];  
  8.     NSURLSessionDownloadTask *downloadTask = [session downloadTaskWithRequest:request  
  9.                                                             completionHandler:  
  10.                                               ^(NSURL *location, NSURLResponse *response, NSError *error) {  
  11.                                                   [self showResponseCode:response];  
  12.                                                     
  13.                                                   // 输出下载文件原来的存放目录  
  14.                                                   NSLog(@"%@", location);  
  15.                                                     
  16.                                                   // 设置文件的存放目标路径  
  17.                                                   NSString *documentsPath = [self getDocumentsPath];  
  18.                                                   NSURL *documentsDirectoryURL = [NSURL fileURLWithPath:documentsPath];  
  19.                                                   NSURL *fileURL = [documentsDirectoryURL URLByAppendingPathComponent:[[response URL] lastPathComponent]];  
  20.                                                     
  21.                                                   // 如果该路径下文件已经存在,就要先将其移除,在移动文件  
  22.                                                   NSFileManager *fileManager = [NSFileManager defaultManager];  
  23.                                                   if ([fileManager fileExistsAtPath:[fileURL path] isDirectory:NULL]) {  
  24.                                                       [fileManager removeItemAtURL:fileURL error:NULL];  
  25.                                                   }  
  26.                                                   [fileManager moveItemAtURL:location toURL:fileURL error:NULL];  
  27.                                                     
  28.                                                   // 在webView中加载图片文件  
  29.                                                   NSURLRequest *showImage_request = [NSURLRequest requestWithURL:fileURL];  
  30.                                                   [self.webView loadRequest:showImage_request];  
  31.                                                     
  32.                                                   [self.spinner stopAnimating];  
  33.                                               }];  
  34.       
  35.     [downloadTask resume];  
  36. }  
  37.   
  38. /* 获取Documents文件夹的路径 */  
  39. - (NSString *)getDocumentsPath {  
  40.     NSArray *documents = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
  41.     NSString *documentsPath = documents[0];  
  42.     return documentsPath;  
  43. }  http://blog.csdn.net/lvchaman/article/details/50296921
0
0 0