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

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

C代码  

  1. - (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace {
  2. return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];
  3. }
  4. - (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
  5. if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust])
  6. //if ([trustedHosts containsObject:challenge.protectionSpace.host])
  7. [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]
  8. forAuthenticationChallenge:challenge];
  9. [challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
  10. }
- (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. TTDCONDITIONLOG(TTDFLAG_URLREQUEST, @"  RECEIVED AUTH CHALLENGE LOADING %@ ", _urlPath);
  9. [_queue loader:self didReceiveAuthenticationChallenge:challenge];
  10. }
  11. //添加
  12. - (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace {
  13. return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];
  14. }
///////////////////////////////////////////////////////////////////////////////////////////////////
- (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];
}
时间: 2024-08-28 16:12:39

ios项目绕过证书访问https程序的相关文章

Https:设置使用证书访问Https

设置证书进行访问或被访问操作 String keyStore = “keyStore文件路径”: String KEY_STORE_PWD = "1234"; String KEY_STORE_TYPE = "PKCS12";//.p12文件类型 String trustStore =“trustStore文件路径”: String TRUST_STORE_PWD = "1234"; //设置客户端证书 System.setProperty(&q

Python自动化测试iOS项目

作为一个开发人员,为了保证自己的代码的健壮,写单元测试是必不可少的环节,然而最痛快的是每天去手动跑一遍所有的case.那么什么能帮我们解决这些繁琐的操作呢,大家应该会想到自动化测试脚本了,是的,我们可以借助脚本来完成全自动化测试,下面是我列的每天脚本自动执行流程: 1.pull git仓库里面的最新代码到本地. 2.然后打包成App. 3.安装到模拟器上. 4.运行App,执行单元测试,生成测试数据并保存到本地. 5.脚本读取测试数据,邮件发送给相关人员. 当这些全自动化后,可以大大减少开发人员

iOS实用技巧 - AFNetworking2安全的使用自签证书访问HTTPS

友情提示:本文使用的AFNetworking是最新git pull的2.3.1版本,如果想确认你机器上的AFNetworking版本,请打git tag命令查看. 绝大部分iOS程序的后台服务都是基于RESTful或者WebService的,不论在任何时候,你都应该将服务置于HTTPS上,因为它可以避免中间人攻击的问题,还自带了基于非对称密钥的加密通道!现实是这些年涌现了大量速成的移动端开发人员,这些人往往基础很差,完全不了解加解密为何物,使用HTTPS后,可以省去教育他们各种加解密技术,生活轻

HTTPS的证书未经权威机构认证的情况下,访问HTTPS站点的两种方法

注意一下文章中提到的jsse在jdk1.4以后已经集成了,不必纠结. 摘 要 JSSE是一个SSL和TLS的纯Java实现,通过JSSE可以很容易地编程实现对HTTPS站点的访问.但是,如果该站点的证书未经权威机构的验证,JSSE将拒绝信任该证书从而不能访问HTTPS站点.本文在简要介绍JSSE的基础上提出了两种解决该问题的方法. 引言 过去的十几年,网络上已经积累了大量的Web应用.如今,无论是整合原有的Web应用系统,还是进行新的Web开发,都要求通过编程来访问某些Web页面.传统的方法是使

HTTPS安全证书访问连接知识讲解

HTTPS安全证书访问连接知识讲解 01:网络安全涉及的问题: ①. 网络安全问题-数据机密性问题 传输的数据可能会被第三方随时都能看到 ②. 网络安全问题-数据完整性问题 传输的数据不能随意让任何人进行修改 ③. 网络安全问题-身份验证问题 第一次通讯时,需要确认通讯双方的身份正确 02:网络安全涉及的问题解决: ①. 网络安全问题-数据机密性问题解决 a) 利用普通加密算法解决机密性 利用相应算法,对传输数据(明文数据)进行加密(密文数据):再利用对应算法,将加密数据解密变为 真实数据 优点

Java中创建访问HTTPS的自签名证书的方法

一.问题: 常常在用java访问https的请求时,总是出现SSL禁止的异常.这里给大家教下怎么创建与添加证书. 二.工具 : 1.创建一个目录 2.去Github上搜索InstallCert,然后随便下载一下来.比如:https://github.com/escline/InstallCert 把java文件下载到刚创建的这个目录中. 三.代码: 比如我要添加访问azure的几个管理的域名的https.. 就这样: 1.新建一个cert.bat文件. 2.bat中的内容如下: javac -d

如何监听第三方应用程序(SOAP or RESTful 客户端)访问HTTPS网站时的数据?

随着互联网的应用越来越多,在我们的日常开发和调试当中(比如调试SOAP和RESTFul的时候),我们常常需要访问用第三方的工具访问HTTPS的网站,为了简化描述,本文使用IE浏览器访问Google 提供的https://www.googleapis.com/discovery/v1/apis RESTful服务为例,注意这个是基于https协议的访问,如果用第三方的嗅探工具,比如wiresharp,即使我们能把http的数据包抓到,显示出来的内容也是乱码,因为传送的内容经过了加密,而加密的私钥就

nginx配置免费ssl证书支持https安全访问

1.自行颁发不受浏览器信任的SSL证书: HTTPS的SSL证书可以自行颁发,Linux下的颁发步骤如下: openssl genrsa -des3 -out www.aaa.com.key 1024 openssl req -new -key www.aaa.com.key -out www.aaa.com.csr openssl rsa -in www.aaa.com.key -out www.aaa.com_nopass.key Nginx.conf的SSL证书配置,使用www.aaa.c

关于httpclient 请求https (如何绕过证书验证)

第一种方法,适用于httpclient4.X 里边有get和post两种方法供你发送请求使用.导入证书发送请求的在这里就不说了,网上到处都是 import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.n