objc跳过安全认证,请求.https地址

Objective-C HTTPS 请求绕过认证
本文介绍如何在 Objective-C 中绕过 HTTPS 安全认证进行网络请求,提供了三种方法:使用 NSURLConnection 的私有方法、MKNetworkingKit 库设置及 AFNetworking 的宏定义和属性配置。

 

objc跳过安全认证,请求.https地址 

收集了几种方法,用于objc中跳过安全认证,请求https形式的地址。

1、NSURLRequest方式(未测试)

 

 

[objc] view plain copy

 在CODE上查看代码片派生到我的代码片

  1. // Created by Alexandre Colucci on 23/07/2008.  
  2. // Copyright 2008 Alexandre Colucci. All rights reserved.  
  3.   
  4. // Dummy interface to avoid a warning.  
  5. @interface NSURLRequest (DummyInterface)  
  6. + (BOOL)allowsAnyHTTPSCertificateForHost:(NSString*)host;  
  7. + (void)setAllowsAnyHTTPSCertificate:(BOOL)allow forHost:(NSString*)host;  
  8. @end  
  9.   
  10. @implementation MainController  
  11.   
  12. -(IBAction)doSomething:(id)sender  
  13. {  
  14.     // The URL of the Webserver  
  15.     NSURL *myWebserverURL = [NSURL URLWithString:@"https://myWebserver.com/"];  
  16.       
  17.     // Create the request  
  18.     NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:myWebserverURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60.0];  
  19.       
  20.     // Set the HTTP method.  
  21.     [theRequest setHTTPMethod:@"POST"];  
  22.       
  23.     // Set useful headers  
  24.     [theRequest setValue:@"text/xml" forHTTPHeaderField:@"Accept"];  
  25.     [theRequest setValue:@"application/xml" forHTTPHeaderField:@"Content-type"];  
  26.       
  27.     // The body  
  28.     NSString *theDataString = @"<?xml version=\"1.0\" encoding=\"UTF-8\"?><something></something>";  
  29.     NSData *theData = [theDataString dataUsingEncoding:NSUTF8StringEncoding];  
  30.     [theRequest setHTTPBody:theData];  
  31.       
  32.     // Use the private method setAllowsAnyHTTPSCertificate:forHost:  
  33.     // to not validate the HTTPS certificate.  
  34.     [NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[myWebserverURL host]];  
  35.       
  36.     // Create the NSURLConnection and init the request.  
  37.     [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];  
  38. }  


2、MKNetworkingKit方式:(测试通过)

 

在中添加一下设置:

 

[objc] view plain copy

 在CODE上查看代码片派生到我的代码片

  1. MKNetworkEngine *netEngine = [[MKNetworkEngine alloc] initWithHostName:@"your host name"];  
  2. MKNetworkOperation *op = [netEngine operationWithURLString:url params:params httpMethod:@"POST"];  
  3. //skip ssl auth  
  4. op.shouldContinueWithInvalidCertificate = YES;  


shouldContinueWithInvalidCertificate默认为NO.

 

源码如下:

 

 

[objc] view plain copy

 在CODE上查看代码片派生到我的代码片

  1. /*! 
  2.  *  @abstract Boolean variable that states whether the operation should continue if the certificate is invalid. 
  3.  *  @property shouldContinueWithInvalidCertificate 
  4.  * 
  5.  *  @discussion 
  6.  * If you set this property to YES, the operation will continue as if the certificate was valid (if you use Server Trust Auth) 
  7.  *  The default value is NO. MKNetworkKit will not run an operation with a server that is not trusted. 
  8.  */  
  9. @property (nonatomic,assign) BOOL shouldContinueWithInvalidCertificate;  


3、AFNetworking:(未亲测)

 

方法一:

添加宏定义,允许https非安全访问

 

[objc] view plain copy

 在CODE上查看代码片派生到我的代码片

  1. #define AFNETWORKING_ALLOW_INVALID_SSL_CERTIFICATES  

 

 

方法二:

 

[objc] view plain copy

 在CODE上查看代码片派生到我的代码片

  1. AFJSONRequestOperation * op = [AFJSONRequestOperation JSONRequestOperationWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:jsonURL]] success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {  
  2.         DLog(@"%@", JSON);  
  3.     } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {  
  4.         DLog(@"%@", error);  
  5.     }];  
  6.     op.allowsInvalidSSLCertificate = YES;  
  7.     [op start];  
  8.  

转载于:https://my.oschina.net/fadoudou/blog/731675

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值