ios项目绕过证书访问https程序_移动开发_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > 移动开发 > ios项目绕过证书访问https程序

ios项目绕过证书访问https程序

 2014/11/6 15:12:51  wx0123  程序员俱乐部  我要评论(0)
  • 摘要:如果是单个的webview或者request请求,在请求的文件h中直接实现NSURLConnectionDelegate,并在m中添加下列实现下列两个方法:C代码-(BOOL)connection:(NSURLConnection*)connectioncanAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace*)protectionSpace{return[protectionSpace
  • 标签:程序 iOS 项目 HTTP

如果是单个的webview或者request请求,在请求的文件h中直接实现NSURLConnectionDelegate,并在m中添加下列实现下列两个方法:

 

 

C代码 复制代码 class="star" src="/Upload/Images/2014110615/40B102E0EF997EA6.png" alt="收藏代码" />spinner" style="display: none;" src="/Upload/Images/2014110615/4E072B8B8C20032D.gif" alt="" />
  1. - (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace {  
  2.     return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];  
  3. }  
  4.   
  5. - (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {  
  6.     if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust])  
  7.         //if ([trustedHosts containsObject:challenge.protectionSpace.host])  
  8.             [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]   
  9.                  forAuthenticationChallenge:challenge];    
  10.     [challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];  
  11. }  
- (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];
}

 上述方法很不爽的是每个请求的地方都要加。

 

如果项目中用到了three20,直接修改“TTRequestLoader.m”即可,这个文件本身就已经实现了NSURLConnectionDelegate,

 

修改如下,只改这一个地方就行,处处可用。

 

 

C代码 复制代码 收藏代码
  1. ///////////////////////////////////////////////////////////////////////////////////////////////////  
  2. - (void)connection:(NSURLConnection *)connection  
  3. didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge{  
  4.     //修改  
  5.     if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust])  
  6.         [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];  
  7.     [challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];  
  8.       
  9.     TTDCONDITIONLOG(TTDFLAG_URLREQUEST, @"  RECEIVED AUTH CHALLENGE LOADING %@ ", _urlPath);  
  10.     [_queue loader:self didReceiveAuthenticationChallenge:challenge];  
  11. }  
  12.   
  13. //添加  
  14. - (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace {  
  15.     return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];  
  16. }  
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)connection:(NSURLConnection *)connection
didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge{
    //修改
    if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust])
        [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
    [challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
    
    TTDCONDITIONLOG(TTDFLAG_URLREQUEST, @"  RECEIVED AUTH CHALLENGE LOADING %@ ", _urlPath);
    [_queue loader:self didReceiveAuthenticationChallenge:challenge];
}

//添加
- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace {
    return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];
}

 

发表评论
用户名: 匿名