关键代码:使用后台下载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服务器的更多相关文章

  1. Web API集成Azure AD认证

    1.声明的介绍 基于角色的授权管理,适用于角色变化不大,并且用户权限不会频繁更改的场景. 在更复杂的环境下,仅仅通过给用户分配角色并不能有效地控制用户访问权限. 基于声明的授权有许多好处,它使认证和授 ...

  2. HTTP认证与https简介

    HTTP请求报头: Authorization HTTP响应报头: WWW-Authenticate  HTTP认证是基于质询/回应(challenge/response)的认证模式. HTTP认证 ...

  3. HTTP请求响应过程以及与HTTPS区别

    HTTP协议 HTTP协议主要应用是在服务器和客户端之间,客户端接受超文本. 服务器按照一定规则,发送到客户端(一般是浏览器)的传送通信协议.与之类似的还有文件传送协议(file transfer p ...

  4. 【Azure 事件中心】使用Azure AD认证方式创建Event Hub Consume Client + 自定义Event Position

    问题描述 当使用SDK连接到Azure Event Hub时,最常规的方式为使用连接字符串.这种做法参考官网文档就可成功完成代码:https://docs.azure.cn/zh-cn/event-h ...

  5. AD认证

    这两天接触到一个新的知识点,AD验证.什么是AD验证?Active Directory——活动目录,活动目录只是LDAP的一个实现,提供LDAP认证.Radius认证和NTML认证,都是标准认证方式 ...

  6. SharePoint2013基于Form(FBA)的AD认证登陆

    来源于:http://www.haogongju.net/art/1964313 1. 使用SharePoint2013实现基于AD的Form认证,首先创建一个Web Application,步骤如下 ...

  7. # openVPN+LDAP AD认证,组权限管理

    # openVPN+LDAP AD认证,组权限管理 原创内容http://www.cnblogs.com/elvi/p/7661178.html # openVPN+LDAP AD认证,组权限管理 # ...

  8. Git认证方式https和ssh的原理及比较

    常见的代码托管平台GitHub.GitLab和BitBucket等,基本都会使用Git作为版本控制工具.平台一般都提供两种认证方式https和ssh.了解该过程能够更加自由的配置和使用,本文就来简单聊 ...

  9. laravel5.7 前后端分离开发 实现基于API请求的token认证

    最近在学习前后端分离开发,发现 在laravel中实现前后台分离是无法无法使用 CSRF Token 认证的.因为 web 请求的用户认证是通过Session和客户端Cookie的实现的,而前后端分离 ...

随机推荐

  1. uploadify 3.2 后台动态传参数

    最近在弄一个上传的小功能,需要往后台传递一些动态参数,网上有一些传参数可能是因为版本不对也没成功.仔细看了官网的一些说明,搞定了. 3.2中传递参数用的的是'formData':{'somekey': ...

  2. [IT学习]关于minidump

    windows debug里面需要用到数据分析. 很重要的一个工具就是dump. 什么事minidump,就是windows在蓝屏或其他故障时,转存的内存数据.(我现在是这么理解的) Applicat ...

  3. [ZZ] RGBM and RGBE encoding for HDR

    Deferred lighting separate lighting rendering and make lighting a completely image-space technique. ...

  4. PHP+jQuery 列表分页类 ( 支持 url 分页 / ajax 分页 )

    /* ******* 环境:Apache2.2.8 ( 2.2.17 ) + PHP5.2.6 ( 5.3.3 ) + MySQL5.0.51b ( 5.5.8 ) + jQuery-1.8.3.mi ...

  5. Gmail 启用 POP 标准配置说明:

    接收邮件 (POP3) 服务器 - 要求 SSL:pop.gmail.com使用 SSL:是端口:995 发送邮件 (SMTP) 服务器 - 要求 TLS 或 SSL:smtp.gmail.com使用 ...

  6. PHP不能创建csv中文名文件

    使用iconv 将utf-8转换成gb2312即可$name = iconv("utf-8","gb2312",'分销商下载课件记录');

  7. Configuration.ConfigurationSettings.AppSettings已过时

    1.在项目中引用System.Configuration.dll,在需要的页面加上using System.Configuration; 2.把ConfigurationSettings.AppSet ...

  8. javaWeb中servlet开发(2)——servlet与表单

    1.重写doGet方法 public class InputServlet extends HttpServlet{ public void doGet(HttpServletRequest req, ...

  9. vs2013 RTM 激活码

    BWG7X-J98B3-W34RT-33B3R-JVYW9

  10. ecshop换用redis做缓存

    <?php /** * ecshop SESSION 保存类 * ================================================================ ...