URLConnection同步,异步与SSL同步请求

NSURLConnection同步,异步与SSL 
    同步请求

        NSURL *url=[[NSURL alloc]initWithString:urlString];
NSMutableURLRequest  *request=[[NSMutableURLRequest alloc]init];
NSError *err=nil;
NSData *data=[NSURLConnection sendSynchronousRequest:request
   returningResponse:nil
   error:&err];
if(data==nil)
{
//if([err code])

NSLog(@"Code:%d,domain:%@,localizedDesc:%@",[err code],
  [err domain],[err localizedDescription]);
}
else
{
}
      这种情况,通过一个静态方法,请求request,这种情况下,会一直阻塞,等到返回结果,简单易用
       异步请求
NSURL *url=[[NSURL alloc]initWithString:urlString];
NSMutableURLRequest  *request=[[NSMutableURLRequest alloc]init];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[url release];
[request release];
if(connection)
{
        receivedData = [[NSMutableData data] retain];
        NSLog(@"intial done!");
    }
else
{
        NSLog(@"sorry");
    }
        通过一个delegate来做数据的下载以及Request的接受等等消息,此处delegate:self,所以需要本类实现一些方法,并且定义receivedData做数据的接受
        基本上要实现下面节歌方法
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSLog(@"get the whole response");
    [receivedData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
NSLog(@"get some data");
    [receivedData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
[connection release];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    [connection release];
   NSLog(@"Connection failed! Error - %@ %@",  
          [error localizedDescription],  
         [[error userInfo] objectForKey:NSErrorFailingURLStringKey]);
}
    基本上这样就搞定了!!!
    但是异步模式下带来了一个新的问题,很多情况下,网络请求不在主线程,或者界面等待网络结果,不在主线程的时候,调用线程如果生命周期over,下面这些可能都没有调用到,导致得不到想要得效果,所以需要在NSURLConnection请求后面加点东西来阻塞
    while(!finished) {
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
}

    好了,现在我们看看SSL的问题,在NSURLConnnection本来有方法可以跳过ssl检查,可惜被apple无情的私有了,所以同步的数据请求肯定不行了,看看文档,只能通过异步delegate的方式了
    - (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace
    {
return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];
    }
    - (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
    {
if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust])
if ([trustedHosts containsObject:challenge.protectionSpace.host])
[challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
[challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
    }
    第一个方法会根据你的URL来判断是否需要做认证
    第二个方法是认证的过程,if ([trustedHosts containsObject:challenge.protectionSpace.host]),这行代码注释掉,就可以自动所有SSL通过,否则,你可以加一些Trust的hosts,其他的不通过就行了!!!
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值