NSURLConnection详解

  1. 通常在IPhone里用网络访问会用到NSURLConnection来做url链接请求,下面简单介绍一下:  
  2. 1、同步请求  
  3.              NSURL *url=[[NSURL alloc]initWithString:urlString];  
  4.     NSMutableURLRequest  *request=[[NSMutableURLRequest alloc]init];  
  5.   
  6.   
  7.     NSError *err=nil;  
  8.   
  9.     NSData *data=[NSURLConnection sendSynchronousRequest:request  
  10.   
  11.                                       returningResponse:nil   
  12.   
  13.                                                   error:&err];  
  14.   
  15.     if(data==nil)  
  16.   
  17.     {  
  18.   
  19.         //if([err code])  
  20.   
  21.           
  22.         NSLog(@"Code:%d,domain:%@,localizedDesc:%@",[err code],  
  23.   
  24.               [err domain],[err localizedDescription]);  
  25.   
  26.     }  
  27.   
  28.     else  
  29.   
  30.     {  
  31.   
  32.     }  
  33.   
  34.   这种情况,通过一个静态方法,请求request,这种情况下,会一直阻塞,等到返回结果,简单易用  
  35.   
  36.   
  37.   
  38.   
  39. 2、异步请求  
  40.   
  41.   
  42.     NSURL *url=[[NSURL alloc]initWithString:urlString];  
  43.     NSMutableURLRequest  *request=[[NSMutableURLRequest alloc]init];  
  44.   
  45.   
  46.     NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];  
  47.   
  48.     [url release];  
  49.   
  50.     [request release];  
  51.   
  52.     if(connection)  
  53.   
  54.     {  
  55.   
  56.             receivedData = [[NSMutableData data] retain];  
  57.   
  58.             NSLog(@"intial done!");  
  59.   
  60.         }  
  61.   
  62.     else  
  63.   
  64.     {  
  65.   
  66.             NSLog(@"sorry");  
  67.   
  68.         }  
  69.   
  70. 通过一个delegate来做数据的下载以及Request的接受等等消息,此处delegate:self,所以需要本类实现一些方法,并且定义receivedData做数据的接受  
  71.   
  72. 基本上要实现下面节歌方法  
  73.   
  74.   
  75.   
  76.   
  77.   
  78.     - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response   
  79.   
  80.     {  
  81.   
  82.         NSLog(@"get the whole response");  
  83.   
  84.             [receivedData setLength:0];  
  85.   
  86.     }  
  87.   
  88.   
  89.   
  90.     - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data  
  91.   
  92.      {  
  93.   
  94.         NSLog(@"get some data");  
  95.   
  96.         [receivedData appendData:data];  
  97.   
  98.     }  
  99.   
  100.   
  101.   
  102.     - (void)connectionDidFinishLoading:(NSURLConnection *)connection   
  103.   
  104.     {  
  105.   
  106.         [connection release];  
  107.   
  108.       
  109.      
  110.     }  
  111.   
  112.   
  113.   
  114.   
  115.     -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error  
  116.   
  117.     {  
  118.   
  119.       
  120.             [connection release];  
  121.   
  122.          
  123.         NSLog(@"Connection failed! Error - %@ %@",     
  124.   
  125.                 [error localizedDescription],          
  126.   
  127.             [[error userInfo] objectForKey:NSErrorFailingURLStringKey]);  
  128.   
  129.     }  
  130.   
  131.   
  132. 基本上这样就搞定了!!!  
  133.   
  134.   
  135. 但是异步模式下带来了一个新的问题,很多情况下,网络请求不在主线程,或者界面等待网络结果,不在主线程的时候,调用线程如果生命周期over,下面这些可能都没有调用到,导致得不到想要得效果,所以需要在NSURLConnection请求后面加点东西来阻塞  
  136. while(!finished) {  
  137.   
  138.           
  139.         [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];  
  140.   
  141.           
  142.     }     
  143.   
  144.   
  145.   
  146.   
  147.   
  148.   
  149.   
  150. 好了,现在我们看看SSL的问题,在NSURLConnnection本来有方法可以跳过ssl检查,可惜被apple无情的私有了,所以同步的数据请求肯定不行了,看看文档,只能通过异步delegate的方式了  
  151.   
  152.   
  153. - (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace   
  154.   
  155. {  
  156.   
  157.     return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];  
  158.   
  159. }  
  160.   
  161.   
  162.   
  163. - (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge   
  164.   
  165. {  
  166.   
  167.     if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust])  
  168.   
  169.     if ([trustedHosts containsObject:challenge.protectionSpace.host])  
  170.   
  171.         [challenge.sender useCredential:[NSURLCredential    credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];  
  172.   
  173.       
  174.     [challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];  
  175.   
  176. }  
  177.   
  178.   
  179.   
  180.   
  181. 第一个方法会根据你的URL来判断是否需要做认证  
  182.   
  183. 第二个方法是认证的过程,if ([trustedHosts containsObject:challenge.protectionSpace.host]),这行代码注释掉,就可以自动所有SSL通过,否则,你可以加一些Trust的hosts,其他的不通过就行了!!!  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值