iOS https请求 NSURLSessionDataTask
//
// YKSHttpsRequest.m
// YKShareSdkDemo
//
// Created by qingyun on 22/05/2017.
// Copyright © 2017 qingjoin. All rights reserved.
//
#import "YKSHttpsRequest.h"
@implementation YKSHttpsRequest
+ (YKSHttpsRequest *)requestWithString:(NSString *)urlString {
return [[YKSHttpsRequest alloc] initWithURLString:urlString];
}
- (id)initWithURLString:(NSString *)urlString {
if (self = [super init]) {
//转码成UTF8
urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
url = [NSURL URLWithString:urlString] ;
NSLog(@"httpurl:%@",url);
request = [NSURLRequest requestWithURL:url];
}
return self;
}
-(void)start
{
//2.创建请求对象
//3.创建session
if(!request)
{
NSLog(@"requestNULL");
}
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[NSOperationQueue mainQueue]];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (error) {
NSLog(@"NSURLSessionDataTaskerror:%@",error);
} else {
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
NSLog(@"NSURLSessionDataTaskdic:%@",dic);
}
//5.解析数据
NSLog(@"NSURLSessionDataTask:%@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]);
}];
//4.执行task
[dataTask resume];
http://www.jianshu.com/p/8ff7269f2eff
}
//post请求
+ (void)postWithUrlString:(NSString *)url parameters:(id)parameters
{
NSURL *nsurl = [NSURL URLWithString:url];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:nsurl];
//设置请求方式
request.HTTPMethod = @"POST";
NSString *postStr = [YKSHttpsRequest parseParams:parameters];
//设置请求体
request.HTTPBody = [postStr dataUsingEncoding:NSUTF8StringEncoding];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:queue];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (error) {
// failureBlock(error);
} else {
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
//successBlock(dic);
NSLog(@"NSURLSessionDataTaskdic:%@",dic);
}
}];
[dataTask resume];
}
//把NSDictionary解析成post格式的NSString字符串
+ (NSString *)parseParams:(NSDictionary *)params
{
NSString *keyValueFormat;
NSMutableString *result = [NSMutableString new];
NSMutableArray *array = [NSMutableArray new];
//实例化一个key枚举器用来存放dictionary的key
NSEnumerator *keyEnum = [params keyEnumerator];
id key;
while (key = [keyEnum nextObject]) {
keyValueFormat = [NSString stringWithFormat:@"%@=%@&", key, [params valueForKey:key]];
[result appendString:keyValueFormat];
[array addObject:keyValueFormat];
}
return result;
}
-(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler
{
NSLog(@"URLSession :%@",challenge.protectionSpace);
if (![challenge.protectionSpace.authenticationMethod isEqualToString:@"NSURLAuthenticationMethodServerTrust"])
return;
// 如果是请求证书信任,我们再来处理,其他的不需要处理
if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust])
{
NSURLCredential *cre = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
// 调用block
completionHandler(NSURLSessionAuthChallengeUseCredential,cre);
//SecTrustRef serverTrust = challenge.protectionSpace.serverTrust;
//completionHandler(NSURLSessionAuthChallengeUseCredential, [NSURLCredential credentialForTrust:serverTrust]);
} else
{
completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, nil);
}
/*
NSURLSessionAuthChallengeUseCredential 使用证书
NSURLSessionAuthChallengePerformDefaultHandling 忽略证书 默认的做法
NSURLSessionAuthChallengeCancelAuthenticationChallenge 取消请求,忽略证书
NSURLSessionAuthChallengeRejectProtectionSpace 拒绝,忽略证书
*/
NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
completionHandler(NSURLSessionAuthChallengeUseCredential,credential);
}
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data {
NSLog(@"URLSessionXXXX%@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]);
}
@end
iOS https请求 NSURLSessionDataTask的更多相关文章
- iOS使用自签名证书实现HTTPS请求
概述 在16年的WWDC中,Apple已表示将从2017年1月1日起,所有新提交的App必须强制性应用HTTPS协议来进行网络请求. 默认情况下非HTTPS的网络访问是禁止的并且不能再通过简单粗暴的向 ...
- iOS ASIHTTPRequest 请求https
iOS 终端请求服务端数据时,为了保证数据安全,我们一般会使用https协议加密,而对于iOS的网络编程,我们一般会使用开源框架:ASIHTTPRequest,但是如果使用传统的http方式,即使忽略 ...
- 为什么ios手机安装好fiddler证书/charles证书还是抓不到https请求?
为什么ios手机安装好fiddler证书/charles证书还是抓不到https请求? 最近有不少人有此困惑, 因为你的ios系统应该是10.0以上的系统, 在手机系统设置---关于手机----证书信 ...
- fiddler抓取https请求(android/ios)
本文转载自:http://blog.csdn.net/songer_xing/article/details/53841401 备注:本人有这样的一个需求,先记录下,以后再进行整理. 在抓包过程中发现 ...
- iOS 10.3下使用Fiddler抓取HTTPS请求
最近使用Fiddler抓取手机上的HTTPS请求时,遇到一个问题.设置完之后,访问HTTPS的页面,却得到一个错误 网络出错,轻触屏幕重新加载:-1202 (如下图所示) 我印象中,之前都好好的, ...
- 十分钟学会Charles抓包(iOS的http/https请求)
### 原文地址,感谢作者 : http://www.jianshu.com/p/5539599c7a25 Charles安装 HTTP抓包 HTTPS抓包 1. Charles安装 官网下载安装Ch ...
- 43.Charles抓包(iOS的http/https请求)
Charles安装 HTTP抓包 HTTPS抓包 1. Charles安装 官网下载安装Charles: https://www.charlesproxy.com/download/ 2. H ...
- iOS 用自签名证书实现 HTTPS 请求的原理
在16年的WWDC中,Apple已表示将从2017年1月1日起,所有新提交的App必须强制性应用HTTPS协议来进行网络请求.默认情况下非HTTPS的网络访问是禁止的并且不能再通过简单粗暴的向Info ...
- Charles抓包(iOS的http/https请求)
Charles抓包(iOS的http/https请求) Charles安装 HTTP抓包 HTTPS抓包 1. Charles安装 官网下载安装Charles:https://www.charlesp ...
随机推荐
- sql 把一个用逗号分隔的多个数据字符串变成一个表的一列
USE [tms]GO/****** Object: UserDefinedFunction [dbo].[StrToTable] Script Date: 2017/4/26 9:06:20 *** ...
- spring cloud (二、服务注册安全demo_eureka)
spring cloud (一.服务注册demo_eureka) 加强服务的安全性,我们接下来加上访问的账号密码: 首先需要添加对应的依赖 <!--账号密码认证依赖--> <depe ...
- Python GUI - tkinter
目录: Tkinter 组件 标准属性 几何管理 代码实例: 1. Label & Button 2. Entry & Text 3.Listbox列表 4.Radiobutton单选 ...
- day30 网络编程 之进程,线程,协程
进程点进去 线程点进去 协程点进去 什么是进程 进程(有时称为重量级进程)是一个执行中的程序.每个进程都拥有自己的地址空间.内存.数据栈以及其他用于跟踪执行的辅助数据.同一个程序执行两次,属于是两个不 ...
- TCP、UDP和HTTP区别
http:是用于www浏览的一个协议.tcp:是机器之间建立连接用的到的一个协议. 1.TCP/IP是个协议组,可分为三个层次:网络层.传输层和应用层.在网络层有IP协议.ICMP协议.ARP协议.R ...
- 爬虫3 requests基础2 代理 证书 重定向 响应时间
import requests # 代理 # proxy = { # 'http':'http://182.61.29.114.6868' # } # res = requests.get('http ...
- 给力的移动 FZU - 2287
你的弟弟给你安排了一个任务,他给了你1到N个数字的乱序排列,现在你想给你的弟弟秀一波操作,操作最少的次数把序列变成1到N的顺序排列,每次操作你可以选择序列中的一个数字并把它移动到序列的头部或尾部. I ...
- DDL DML概念 --- Msysql常用命令
一.DML DML(data manipulation language)数据操纵语言: 就是我们最经常用到的 SELECT.UPDATE.INSERT.DELETE. 主要用来对数据库的数据进行一些 ...
- linux下安装node踩坑总结
1.在node官网下载linux二进制文件(确定文件的类型)本文以二进制文件为例 2.放入linux的对应目录下之后: tar -xvf node-v10.15.3-linux-x64.tar.xz ...
- C# 动态调用WebService 2
using Microsoft.CSharp; using System; using System.CodeDom; using System.CodeDom.Compiler; using Sys ...