参考博客:
http://blog.csdn.net/github_34613936/article/details/51490032
NSURLSession自签名
NSString *urlString = @"https://192.168.64.43:443/get/test";
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10.0f];
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[NSOperationQueue mainQueue]];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (error) {
NSLog(@"-->%@",error.description);
return;
}
NSString *str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"success -->%@",str);
}];
[task resume];
#设置NSURLSessionDataDelegate委托,实现方向如下
-(void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler {
// 判断是否是信任服务器证书
if(challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust) {
// 告诉服务器,客户端信任证书
// 创建凭据对象
NSURLCredential *credntial = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
// 通过completionHandler告诉服务器信任证书
completionHandler(NSURLSessionAuthChallengeUseCredential,credntial);
}
NSLog(@"protectionSpace = %@",challenge.protectionSpace);
}
AFNetWorking自签名
NSString *urlStr = @"https://192.168.64.43:443/get/test";
//获取证书信息
NSString *path = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"cer"];
NSData *data = [NSData dataWithContentsOfFile:path];
NSSet *set = [[NSSet alloc]initWithObjects:data, nil];
AFSecurityPolicy *policy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate];
//允许证书
policy.allowInvalidCertificates = YES;
//不需要验证域名
policy.validatesDomainName = NO;
//设置证书
policy.pinnedCertificates = set;
//请求网络
AFHTTPSessionManager *mg = [AFHTTPSessionManager manager];
//设置安全政策
mg.securityPolicy = policy;
//配置返回数据和发送数据的格式
mg.responseSerializer = [AFHTTPResponseSerializer serializer];
mg.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil,nil];
mg.requestSerializer = [AFJSONRequestSerializer serializer];
mg.requestSerializer.timeoutInterval = 10.0f;
NSDictionary *dict = @{@"wang":@"123"};
[mg POST:urlStr parameters:dict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
// id result = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
NSString *str = [[NSString alloc]initWithData:responseObject encoding:NSUTF8StringEncoding];
NSLog(@"success -->%@",str);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"error -->%@",error.description);
}];
TestHttps
- (void)testHttps {
NSString *urlString = @"https://192.168.64.43:8080/image/main.jpg";
NSString *cerPath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"cer"];
NSData * certData =[NSData dataWithContentsOfFile:cerPath];
NSSet * certSet = [[NSSet alloc] initWithObjects:certData, nil];
AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate];
// 是否允许,NO-- 不允许无效的证书
[securityPolicy setAllowInvalidCertificates:YES];
securityPolicy.validatesDomainName = NO;
// 设置证书
[securityPolicy setPinnedCertificates:certSet];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.securityPolicy = securityPolicy;
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
manager.requestSerializer.timeoutInterval = 10;
// request
[manager GET:urlString parameters:nil progress:^(NSProgress * progress){
} success:^(NSURLSessionDataTask *task, id responseObject) {
NSString *str = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSLog(@"OK === %@",str);
} failure:^(NSURLSessionDataTask *task, NSError *error) {
NSLog(@"error ==%@",error.description);
}];
}