(1)http://blog.csdn.net/rongxinhua/article/details/20079607?utm_source=tuicool&utm_medium=referral
(2)http://blog.csdn.net/majiakun1/article/details/38133703
开发iOS应用要调用Http接口、获取Http资源,有一套比较成熟的框架ASIHTTPRequest。而我还是比较喜欢使用原始一点的API,而它跟其他的面向对象语言有许多共通之处。本文分同步请求和异步请求这两种情况来讲解一下Http API的使用。直接上代码,注释即文档!
同步请求:即发起Http请求、获取并处理返回值都在同一个线程中进行
- //创建URL对象
- NSString *urlStr = @"http://blog.csdn.net/rongxinhua";
- NSURL *url = [[NSURL alloc] initWithString:urlStr];
- //创建HTTP请求
- //方法1(注:NSURLRequest只支持Get请求,NSMutableURLRequest可支持Get和Post请求)
- NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
- NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
- //方法2,使用工厂方法创建
- NSURLRequest *request = [NSURLRequest requestWithURL:url];
- NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
- //同时设置缓存策略和超时时间
- NSMutableURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:15];
- //设置Http头
- NSDictionary *headers = [request allHTTPHeaderFields];
- [headers setValue:@"iOS-Client-ABC" forKey:@"User-Agent"];
- //设置请求方法
- [request setHTTPMethod:@"GET"];
- [request setHTTPMethod:@"POST"];
- //设置要发送的正文内容(适用于Post请求)
- NSString *content = @"username=stanyung&password=123";
- NSData *data = [content dataUsingEncoding:NSUTF8StringEncoding];
- [request setHTTPBody:data];
- //同步执行Http请求,获取返回数据
- NSURLResponse *response;
- NSError *error;
- NSData *result = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
- //返数据转成字符串
- NSString *html = [[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding];
- //(如果有错误)错误描述
- NSString *errorDesc = [error localizedDescription];
- //获取状态码和HTTP响应头信息
- NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
- NSInteger statusCode = [httpResponse statusCode];
- NSDictionary *responseHeaders = [httpResponse allHeaderFields];
- NSString *cookie = [responseHeaders valueForKey:@"Set-Cookie"];
注:以上代码,不要Copy直接执行,只是列举Http常用方法的调用。
异步请求:发起HTTP请求在一个线程中,返回结果处理在另一个线程中。相比同步请求,异步请求不需要等待返回结果,当前程序可以继续往下执行。在Objective-C中,异步请求也有两种实现方式:一种是注册回调代理,一种是使用回调代码块。
a.注册回调代理的方式:
- [NSURLConnection connectionWithRequest:request delegate:self];
需要实现NSURLConnectionDataDelegate协议:
- @interface HttpDownloadService : NSObject<NSURLConnectionDataDelegate> {
- }
- NSMutableData *buff; //暂存响应的数据
- bool finished = false; //读完完整标记
- //收到HTTP响应时调用
- -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
- NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
- NSDictionary *headers = [httpResponse allHeaderFields];
- buff = [[NSMutableData alloc] init];
- }
- //读取返回数据时调用(可能会执行多次此方法)
- -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
- [buff appendData:data];
- }
- //读完数据完成时调用
- -(void)connectionDidFinishLoading:(NSURLConnection *)connection {
- NSString *html = [[NSString alloc] initWithData:buff encoding:NSUTF8StringEncoding];
- finished = true;
- }
通常情况下,数据在网络中传输,会受到带宽等因素的影响,并不会一次情将所有数据返回,你可能分几次才能接受完整一个HTTP响应报文。因此,(void)connection:(NSURLConnection *)didReceiveData:(NSData *) 这个方法很可能会执行多次。
上例代码中,使用了NSMutableData来暂存接收到的响应数据片段,每一段并接起来,直到读取完整。
b.使用回调代码块的方式:
- [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:
- ^(NSURLResponse *response, NSData *result, NSError *error){ //只会进入一次,方法内部已经实现了Buffer作用
- NSString *html = [[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding];
- }];
备注1:本文的代码例子将使用ARC编码模式,故所新建的对象均没有显式调用release()方法回收。
备注2:若你测试本例子代码新建的是Command Line Tool工程,在main函数中执行相关代码,上面两种异步执行的情况,你很可能你的程序没有执行到回调方法或回调代码块里面去,这是因为:在main函数中,主线程没有等待阻塞,一下子执行完了,回调代码所在的子线程可能未执行完或者根本还没开始执行,就已经因为主线程的结束而结束了。解决这个问题,可以在调用完异步方法后面,加以下代码:
- while (!finished) {
- [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
- }
=======================
Session Task分为三种Data Task,Upload Task,Download Task。毫无疑问,Session Task是整个NSURLSession架构的核心目标。
下面写了一个简单的Demo来初步使用下三种任务对象。这里使用的是convenience methods,并没有定制session和使用协议,都是采用completionHandler作为回调动作。
故事板内容为:
第一种Data Task用于加载数据,使用全局的shared session和dataTaskWithRequest:completionHandler:方法创建。代码如下:
- /* 使用NSURLSessionDataTask加载网页数据 */
- - (IBAction)loadData:(id)sender {
- // 开始加载数据,让spinner转起来
- [self.spinner startAnimating];
- // 创建Data Task,用于打开我的csdn blog主页
- NSURL *url = [NSURL URLWithString:@"http://blog.csdn.net/u010962810"];
- NSURLRequest *request = [NSURLRequest requestWithURL:url];
- NSURLSession *session = [NSURLSession sharedSession];
- NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
- completionHandler:
- ^(NSData *data, NSURLResponse *response, NSError *error) {
- // 输出返回的状态码,请求成功的话为200
- [self showResponseCode:response];
- // 在webView中加载数据
- [self.webView loadData:data
- MIMEType:@"text/html"
- textEncodingName:@"utf-8"
- baseURL:nil];
- // 加载数据完毕,停止spinner
- [self.spinner stopAnimating];
- }];
- // 使用resume方法启动任务
- [dataTask resume];
- }
- /* 输出http响应的状态码 */
- - (void)showResponseCode:(NSURLResponse *)response {
- NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;
- NSInteger responseStatusCode = [httpResponse statusCode];
- NSLog(@"%d", responseStatusCode);
- }
completionHandler指定任务完成后的动作。注意一定要使用resume方法启动任务。(Upload Task和Download Task同理)
运行结果:
第二种Upload Task用于完成上传文件任务,使用方法类似:
- /* 使用NSURLSessionUploadTask上传文件 */
- - (IBAction)uploadFile:(id)sender {
- // NSURL *URL = [NSURL URLWithString:@"http://example.com/upload"];
- // NSURLRequest *request = [NSURLRequest requestWithURL:URL];
- // NSData *data = ...;
- //
- // NSURLSession *session = [NSURLSession sharedSession];
- // NSURLSessionUploadTask *uploadTask = [session uploadTaskWithRequest:request
- // fromData:data
- // completionHandler:
- // ^(NSData *data, NSURLResponse *response, NSError *error) {
- // // ...
- // }];
- //
- // [uploadTask resume];
- }
第三种Download Task用于完成下载文件的任务,使用全局的shared session和downloadTaskWithRequest:completionHandler:方法创建。
注意:在下载任务完成后,下载的文件位于tmp目录下,由代码块中的location指定(不妨输出看看),我们必须要在completion handler中将文件放到持久化的目录下保存。代码如下:
- /* 使用NSURLSessionDownloadTask下载文件 */
- - (IBAction)downloadFile:(id)sender {
- [self.spinner startAnimating];
- NSURL *URL = [NSURL URLWithString:@"http://b.hiphotos.baidu.com/image/w%3D2048/sign=6be5fc5f718da9774e2f812b8469f919/8b13632762d0f703b0faaab00afa513d2697c515.jpg"];
- NSURLRequest *request = [NSURLRequest requestWithURL:URL];
- NSURLSession *session = [NSURLSession sharedSession];
- NSURLSessionDownloadTask *downloadTask = [session downloadTaskWithRequest:request
- completionHandler:
- ^(NSURL *location, NSURLResponse *response, NSError *error) {
- [self showResponseCode:response];
- // 输出下载文件原来的存放目录
- NSLog(@"%@", location);
- // 设置文件的存放目标路径
- NSString *documentsPath = [self getDocumentsPath];
- NSURL *documentsDirectoryURL = [NSURL fileURLWithPath:documentsPath];
- NSURL *fileURL = [documentsDirectoryURL URLByAppendingPathComponent:[[response URL] lastPathComponent]];
- // 如果该路径下文件已经存在,就要先将其移除,在移动文件
- NSFileManager *fileManager = [NSFileManager defaultManager];
- if ([fileManager fileExistsAtPath:[fileURL path] isDirectory:NULL]) {
- [fileManager removeItemAtURL:fileURL error:NULL];
- }
- [fileManager moveItemAtURL:location toURL:fileURL error:NULL];
- // 在webView中加载图片文件
- NSURLRequest *showImage_request = [NSURLRequest requestWithURL:fileURL];
- [self.webView loadRequest:showImage_request];
- [self.spinner stopAnimating];
- }];
- [downloadTask resume];
- }
- /* 获取Documents文件夹的路径 */
- - (NSString *)getDocumentsPath {
- NSArray *documents = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
- NSString *documentsPath = documents[0];
- return documentsPath;
- }