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

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

1、NSURLRequest方式(未测试)


// Created by Alexandre Colucci on 23/07/2008.
// Copyright 2008 Alexandre Colucci. All rights reserved.

// Dummy interface to avoid a warning.
@interface NSURLRequest (DummyInterface)
+ (BOOL)allowsAnyHTTPSCertificateForHost:(NSString*)host;
+ (void)setAllowsAnyHTTPSCertificate:(BOOL)allow forHost:(NSString*)host;
@end

@implementation MainController

-(IBAction)doSomething:(id)sender
{
	// The URL of the Webserver
	NSURL *myWebserverURL = [NSURL URLWithString:@"https://myWebserver.com/"];
	
	// Create the request
	NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:myWebserverURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60.0];
	
	// Set the HTTP method.
	[theRequest setHTTPMethod:@"POST"];
	
	// Set useful headers
	[theRequest setValue:@"text/xml" forHTTPHeaderField:@"Accept"];
	[theRequest setValue:@"application/xml" forHTTPHeaderField:@"Content-type"];
	
	// The body
	NSString *theDataString = @"<?xml version=\"1.0\" encoding=\"UTF-8\"?><something></something>";
	NSData *theData = [theDataString dataUsingEncoding:NSUTF8StringEncoding];
	[theRequest setHTTPBody:theData];
	
	// Use the private method setAllowsAnyHTTPSCertificate:forHost:
	// to not validate the HTTPS certificate.
	[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[myWebserverURL host]];
	
	// Create the NSURLConnection and init the request.
	[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
}

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

在中添加一下设置:

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

shouldContinueWithInvalidCertificate默认为NO.

源码如下:

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

3、AFNetworking:(未亲测)

方法一:

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


#define AFNETWORKING_ALLOW_INVALID_SSL_CERTIFICATES


方法二:

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







评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值