iOS对https的支持

来源:互联网 发布:天堂2血盟mac版 编辑:程序博客网 时间:2024/06/05 06:29

[objc]
 view plain copy
 在CODE上查看代码片派生到我的代码片
1、NSURLRequest方式

#import "AppDelegate.h"

#import "ViewController.h"


@interfaceAppDelegate ()

@end


@implementation NSURLRequest(ViewController)

//该方法对原生App接口访问及WebView接口访问均可

#pragma mark--允许进行https通信

+ (BOOL)allowsAnyHTTPSCertificateForHost:(NSString *)host {

    return YES;

}

+ (void)setAllowsAnyHTTPSCertificate:(BOOL)allow forHost:(NSString*)host {   

}

@end

2、AFNetworking 
方法一:
AFNetworking的头文件<AFNetworking.h>添加如下宏定义

#define AFNETWORKING_ALLOW_INVALID_SSL_CERTIFICATES 


然后在做网络请求时添加如下代码即可:

 AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManagermanager];

 AFSecurityPolicy *securityPolicy = [AFSecurityPolicydefaultPolicy];

 securityPolicy.allowInvalidCertificates =YES;

 manager.securityPolicy = securityPolicy;


方法二(未测):
  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]; 

0 0
原创粉丝点击