使用NSURLSession请求需要AD认证的HTTPS服务器
关键代码:使用后台下载PDF文件
- (void)startDownloadPDF{
NSURLSession *session = [self session];
NSString *downloadURLString = _fileURL;
NSURL *downloadURL = [NSURL URLWithString:[kGlobal getEncodeUrl:downloadURLString]];
//使用NSMutableURLRequest进行AD认证
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:downloadURL];
NSString *authString = [[[NSString stringWithFormat:@"%@:%@", kGlobal.userInfo.sAccount, kGlobal.userInfo.sPassword] dataUsingEncoding:NSUTF8StringEncoding] base64EncodedString];
authString = [NSString stringWithFormat: @"Basic %@", authString];
[req setValue:authString forHTTPHeaderField:@"Authorization"];
NSURLSessionDownloadTask *task = [session downloadTaskWithRequest:req];
//执行resume 保证开始了任务
[task resume];
}
//后台会话(保证只有一个后台会话)
-(NSURLSession *)session {
static NSURLSession *session;
static dispatch_once_t token;
dispatch_once(&token, ^{
//后台下载
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:@"rich"];
configuration.discretionary = YES;
//使用配置的NSURLSessionConfiguration获取NSSession,并且设置委托
session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
});
return session;
}
//HTTPS认证
- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler{
NSURLSessionAuthChallengeDisposition disposition = NSURLSessionAuthChallengePerformDefaultHandling;
__block NSURLCredential *credential = nil;
if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
if (credential) {
disposition = NSURLSessionAuthChallengeUseCredential;
} else {
disposition = NSURLSessionAuthChallengePerformDefaultHandling;
}
} else {
disposition = NSURLSessionAuthChallengePerformDefaultHandling;
}
if (completionHandler) {
completionHandler(disposition, credential);
}
}
//打印下载过程,下载的百分比
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask
didWriteData:(int64_t)bytesWritten
totalBytesWritten:(int64_t)totalBytesWritten
totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite {
float percent = (float)totalBytesWritten/totalBytesExpectedToWrite;
NSLog(@"%f",percent);
}
//下载完成之后的文件存储在临时目录中,下载完成之后,转移文件的目录
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask
didFinishDownloadingToURL:(NSURL *)location {
// NSString *dirPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
// NSString *path = [dirPath stringByAppendingPathComponent:@"1.mp3"];
//
// NSFileManager *manager = [NSFileManager defaultManager];
// if ([manager fileExistsAtPath:path isDirectory:NO]) {
// [manager removeItemAtPath:path error:nil];
// }
//
// [manager moveItemAtPath:[location path] toPath:path error:nil];
}
//任务结束
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task
didCompleteWithError:(NSError *)error {
NSLog(@"%s:%@",__func__,error);
if (error == NULL) {
self.rightButton.enabled = YES;
}
}
使用NSURLSession请求需要AD认证的HTTPS服务器的更多相关文章
- Web API集成Azure AD认证
1.声明的介绍 基于角色的授权管理,适用于角色变化不大,并且用户权限不会频繁更改的场景. 在更复杂的环境下,仅仅通过给用户分配角色并不能有效地控制用户访问权限. 基于声明的授权有许多好处,它使认证和授 ...
- HTTP认证与https简介
HTTP请求报头: Authorization HTTP响应报头: WWW-Authenticate HTTP认证是基于质询/回应(challenge/response)的认证模式. HTTP认证 ...
- HTTP请求响应过程以及与HTTPS区别
HTTP协议 HTTP协议主要应用是在服务器和客户端之间,客户端接受超文本. 服务器按照一定规则,发送到客户端(一般是浏览器)的传送通信协议.与之类似的还有文件传送协议(file transfer p ...
- 【Azure 事件中心】使用Azure AD认证方式创建Event Hub Consume Client + 自定义Event Position
问题描述 当使用SDK连接到Azure Event Hub时,最常规的方式为使用连接字符串.这种做法参考官网文档就可成功完成代码:https://docs.azure.cn/zh-cn/event-h ...
- AD认证
这两天接触到一个新的知识点,AD验证.什么是AD验证?Active Directory——活动目录,活动目录只是LDAP的一个实现,提供LDAP认证.Radius认证和NTML认证,都是标准认证方式 ...
- SharePoint2013基于Form(FBA)的AD认证登陆
来源于:http://www.haogongju.net/art/1964313 1. 使用SharePoint2013实现基于AD的Form认证,首先创建一个Web Application,步骤如下 ...
- # openVPN+LDAP AD认证,组权限管理
# openVPN+LDAP AD认证,组权限管理 原创内容http://www.cnblogs.com/elvi/p/7661178.html # openVPN+LDAP AD认证,组权限管理 # ...
- Git认证方式https和ssh的原理及比较
常见的代码托管平台GitHub.GitLab和BitBucket等,基本都会使用Git作为版本控制工具.平台一般都提供两种认证方式https和ssh.了解该过程能够更加自由的配置和使用,本文就来简单聊 ...
- laravel5.7 前后端分离开发 实现基于API请求的token认证
最近在学习前后端分离开发,发现 在laravel中实现前后台分离是无法无法使用 CSRF Token 认证的.因为 web 请求的用户认证是通过Session和客户端Cookie的实现的,而前后端分离 ...
随机推荐
- jQuery 找到当前元素之前最后一次出现的某个同辈元素
DOM 树状图如下所示,要找到 div id = 'a' 的元素之前的(同辈)离该 div 最近的一个 div class = 'a' 的元素(图中左至右第 2 个 div class = 'a' 的 ...
- [SHELL实例] (转)最牛B的 Linux Shell 命令 (一)
本文编译自commandlinefu.com ( 应该是 Catonmat ) 的系列文章 Top Ten One-Liners from CommandLineFu Explained .作为一个由 ...
- ActionResult,PartialViewResult,EmptyResult,ContentResult
HttpGet HttpPost HttpDelete HttpPut HeepHead HttpOptions HttpPatch属性都是动作方法选定器的一份子,比如若在action上套用H ...
- WCF中自定义消息编码器:压缩编码器的使用
通过抓包知道WCF在提交.返回数据的时候大多使用XML进行数据交互,如果返回DataTable那么这些数据将变得很大,通过查询找到一个对数据压缩的方法: http://msdn.microsoft.c ...
- c/c++编译时,指定程序运行时查找的动态链接库路径
http://blog.csdn.net/tsxw24/article/details/10220735 c/c++编译时,指定程序运行时查找的动态链接库路径 分类: c/c++ linux 2013 ...
- java编程技巧
欢迎提出建议指出错误互相交流. 1.统计对象数量,比如统计一共发射了多少颗子弹. public class Bullet { public static int count = 0; public B ...
- The Bip Buffer - The Circular Buffer with a Twist
Introduction The Bip-Buffer is like a circular buffer, but slightly different. Instead of keeping on ...
- SQLSERVER 16进制与10进制转换
最近工控项目中遇到的16进制与10进制转换,在.NET中比较容易实现,在SQLSERVER中发现没有直接的转换,尤其是出现超出范围的long负数,即无符号64位整数在sqlserver中的存储.网上找 ...
- GIS 学习及参考站点
地理信息论坛 GIS空间站 GISALL 广东水利厅 flex版的
- LightOj1028 - Trailing Zeroes (I)---求因子个数
题目链接:http://lightoj.com/volume_showproblem.php?problem=1028 题意:给你一个数 n (1<=n<=10^12), 然后我们可以把它 ...