1. HTTPS 证书认证(导入相关证书)

#pragma mark - https认证
- (AFSecurityPolicy*)customSecurityPolicy {
// 先导入证书
NSString *cerPath = [[NSBundle mainBundle] pathForResource:@"xxxx" 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 = YES;
NSSet * set = [NSSet setWithObject:certData];
securityPolicy.pinnedCertificates = set; return securityPolicy;
}

2. 发送请求

   NSMutableDictionary *parameters = [[NSMutableDictionary alloc] initWithCapacity:];
[parameters setObject:@() forKey:@"pagenum"]; [[AFNetAPIClient sharedJsonClient].setRequest(@"getjobs").RequestType(GET).Parameters(parameters) startRequestWithSuccess:^(NSURLSessionDataTask *task, id responseObject) {
NSLog(@"%@",responseObject); UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"JSON"
message: responseObject[@"msg"]
delegate: self
cancelButtonTitle: @"Cancel"
otherButtonTitles: @"OK", nil];
[alert show]; NSArray *array = [responseObject[@"data"][@"pagedata"] mutableCopy];
NSLog(@"数组长度:%ld",array.count);
} progress:^(NSProgress *progress) {
NSLog(@"%@",progress);
} failure:^(NSURLSessionDataTask *task, NSError *error) {
NSLog(@"%@",error);
}];

3. 下载文件

    // 下载文件
[[AFNetAPIClient sharedJsonClient].setRequest(@"http://120.25.226.186:32812/resources/videos/minion_02.mp4") downloadWithSuccess:^(NSURLResponse *response, NSURL *filePath) {
NSLog(@"Success");
} progress:^(NSProgress *progress) {
NSLog(@"%@",progress);
} failure:^(NSURLSessionDataTask *task, NSError *error) {
NSLog(@"%@",error);
}];

4.上传图片

    //上传图片
UIImage *img = [UIImage imageNamed:@""]; NSData *data = UIImageJPEGRepresentation(img, 0.5); NSDictionary *dic = @{@"timestamp" : @"",
@"file" : data,
@"xtype" :@"bang_album",
@"token" : @"8a3dead8022c6c85248efca767c9ecfaf8836617"}; [[AFNetAPIClient sharedJsonClient].setRequest(@"upload.php").Parameters(dic).filedata(data).name(@"Filedata").filename(@"Filedata.jpg").mimeType(@"image/jpeg") uploadfileWithSuccess:^(NSURLSessionDataTask *task, id responseObject) {
NSLog(@"Success");
} progress:^(NSProgress *progress) {
NSLog(@"%@",progress);
} failure:^(NSURLSessionDataTask *task, NSError *error) {
NSLog(@"%@",error);
}];

【链接】https://github.com/MrJalen/AFNet3.0

版权声明:本文为博主原创文章,未经博主允许不得转载。

最新文章

  1. iOS强制横屏
  2. php---apache的A/B测试
  3. 获取mysql数据表中的列名
  4. windows7安装IE11点击图标没反应
  5. C++的辅助工具介绍 [转]
  6. 关于cocos2d-x精灵加亮及变灰效果
  7. shell命令输入输出重定向
  8. layui动态设置checkbox选中状态
  9. Win10系列:C#应用控件进阶5
  10. 快速失败/报错机制 - fail-fast
  11. 转载-HashMap1.7源码分析
  12. Vue -- 项目报错整理(1):RangeError: Maximum call stack size exceeded
  13. Qt Installer Framework 使用说明(三)
  14. HTTP API接口测试利器PostMan介绍
  15. Jmeter 之 ServerAgent 在性能测试的时候通过插件监听数据库状态
  16. Nginx实现404页面的几种方法【转】
  17. 【WPF】影城POS的前世今生
  18. JSX语法规范
  19. ffmepg的基本使用
  20. cport串口控件的应用

热门文章

  1. 2017-2018-2 20155315《网络对抗技术》Exp1:PC平台逆向破解
  2. 2017-2018-1 20155330 《信息安全系统设计基础》加分项目--实现mypwd
  3. 《Java 程序设计》课堂实践项目汇总链接
  4. 5种处理js跨域问题方法汇总
  5. 我们一起来聊聊并发吧,one。
  6. 【日常训练】Help Victoria the Wise(Codeforces 99C)
  7. python笔记--冒泡排序升级版
  8. static笔记
  9. selenium的基本定位方式总结
  10. HTML基础范例