【Objective-C】Http常用API、同步请求与异步请求

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

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

[objc]  view plain copy 在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 plain copy 在CODE上查看代码片 派生到我的代码片
  1. [NSURLConnection connectionWithRequest:request delegate:self];  


需要实现NSURLConnectionDataDelegate协议:

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


实现相关的协议方法:

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

@容新华技术博客 - http://blog.csdn.net/rongxinhua - 原创文章,转载请注明出处

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值