//请求
-(void)requestNet{
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager setSecurityPolicy:[self customSecurityPolicy]];//如2若提到的FactoryUI的类方法
manager.responseSerializer.acceptableContentTypes=[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", @"text/plain",nil];
manager.requestSerializer = [AFHTTPRequestSerializer serializer];//默认是二进制
manager.responseSerializer = [AFHTTPResponseSerializer serializer];//默认是JSON
[manager GET:@"https://101.230.222.94:38080/TboxApi.asmx/UserSignIn_Phone" parameters:@{@"PSD":@"094C8C111C43A48EA91CC3E757319812",@"phoneNum":@"17717934618"} progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id_Nullable responseObject) {
NSString *str = [[NSString alloc]initWithData:responseObject encoding:NSUTF8StringEncoding];
NSLog(@"%@",str);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"访问失败");
}];
}
//支持https
- (AFSecurityPolicy *)customSecurityPolicy
{
//先导入证书,找到证书的路径
NSString *cerPath = [[NSBundle mainBundle] pathForResource:@"sv71" ofType:@"cer"];
NSData *certData = [NSData dataWithContentsOfFile:cerPath];
//AFSSLPinningModeCertificate使用证书验证模式
AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate];
//allowInvalidCertificates是否允许无效证书(也就是自建的证书),默认为NO
//如果是需要验证自建证书,需要设置为YES
securityPolicy.allowInvalidCertificates = YES;
//validatesDomainName是否需要验证域名,默认为YES;
//假如证书的域名与你请求的域名不一致,需把该项设置为NO;如设成NO的话,即服务器使用其他可信任机构颁发的证书,也可以建立连接,这个非常危险,建议打开。
//置为NO,主要用于这种情况:客户端请求的是子域名,而证书上的是另外一个域名。因为SSL证书上的域名是独立的,假如证书上注册的域名是www.google.com,那么mail.google.com是无法验证通过的;当然,有钱可以注册通配符的域名*.google.com,但这个还是比较贵的。
//如置为NO,建议自己添加对应域名的校验逻辑。
securityPolicy.validatesDomainName = NO;