使用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的实现的,而前后端分离 ...
随机推荐
- 《Java核心技术卷二》笔记(一)流与文件
一.流的概念 在Java中,可以提供字节序列的对象,或者可以接收字节序列的对象,都可以抽象成流.系统中的文件,网络,内存这些设备都可以读入或者写入字节,自然也可以用流的方式来操作.能向程序中提供字节序 ...
- [IT思考]技术领先对手多少算好?
坦白讲,很多做技术的公司,总是觉得技术越“牛”越好.但是,历史也曾经出现过,很多公司技术的确很棒,但是市场反响一般(待补充案例).结果可想而知. 要知道,每一个新技术,尤其是业界领先的新技术,需要不少 ...
- 每天php函数 - list()给一组变量赋值
array list ( mixed $varname [, mixed $... ] ) 像 array() 一样,这不是真正的函数,而是语言结构.list()用一步操作给一组变量进行赋值. var ...
- PHP+jQuery 注册模块的改进之一:验证码存入SESSION
/* ******* Date:2014-09-28 ******* Author:小dee ******* Blog:http://www.cnblogs.com/dee0912/*/ 对上一篇博文 ...
- Environment Variables
https://msdn.microsoft.com/en-us/library/windows/desktop/ms682653(v=vs.85).aspx Every process has an ...
- w win cmd VS broswer
php -f Fetch data from db , use php without any bugs to analyse the data and read and write db, with ...
- 浏览器指纹 - HTTP cookie
http://www.iefans.net/ruhe-fangfan-xielu-shangwang-yinsi/ http://www.iefans.net/cookie-yinsi-guanxi/ ...
- SQL Server 触发器【转】
触发器是一种特殊类型的存储过程,它不同于之前的我们介绍的存储过程.触发器主要是通过事件进行触发被自动调用执行的.而存储过程可以通过存储过程的名称被调用. Ø 什么是触发器 触发器对表进行插入.更新.删 ...
- 用正则验证字符串格式,形如:A)XXX B)XXXX C)XXX
今天遇到个小功能,要验证某个英文选项是否正确,例如:A)accumulate B)circling C)communities D)competition E)domestic F)financi ...
- 蓝牙的SDP协议总结
1.概念 SDP协议让客户机的应用程序发现存在的服务器应用程序提供的服务以及这些服务的属性.SDP只提供发现服务的机制,不提供使用这些服务的方法.每个蓝牙设备都需要一个SDP Service, ...