`
gaobusi
  • 浏览: 197399 次
  • 性别: Icon_minigender_1
  • 来自: 安徽
社区版块
存档分类
最新评论

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

    博客分类:
  • IOS
阅读更多

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

 

- (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,

 

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

 

///////////////////////////////////////////////////////////////////////////////////////////////////
- (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];
}
 

 

 

 

 

 

 

 

分享到:
评论
2 楼 gaobusi 2012-09-03  
wangjie1986 写道
你好,按照上面的方法,在wifi网络下可以访问https了
但是,如果换成手机网络怎么都无法访问,请求一直发不出去,URLConnectionDelegate的所有回调都一直没有被调用,楼主知道这个原因吗,是不是手机网络和wifi网络有所不同。


我以前用上面第一个方法也是在wifi环境下测试的没有问题,没注意你说的问题,再找找原因吧。这种方法比较不爽的是每个请求都要加。

后来在three20中用的上述的第二种方法,一切顺利,只需要改three20中的一个文件即可。
1 楼 wangjie1986 2012-08-30  
你好,按照上面的方法,在wifi网络下可以访问https了
但是,如果换成手机网络怎么都无法访问,请求一直发不出去,URLConnectionDelegate的所有回调都一直没有被调用,楼主知道这个原因吗,是不是手机网络和wifi网络有所不同。

相关推荐

Global site tag (gtag.js) - Google Analytics