iOS网络数据指标收集
@interface MySessionDelegate : NSObject <NSURLSessionDelegate> @end @implementation MySessionDelegate
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didFinishCollectingMetrics:(NSURLSessionTaskMetrics *)metrics {
// 打印出请求的各项指标
for(NSURLSessionTaskTransactionMetrics *transaction in metrics.transactionMetrics) {
NSHTTPURLResponse *response = (NSHTTPURLResponse *)transaction.response;
NSLog(@"Request URL: %@", transaction.request.URL);
NSLog(@"HTTP Status Code: %ld", (long)response.statusCode);
NSLog(@"Request Start Time: %@", transaction.startDate);
NSLog(@"Request End Time: %@", transaction.endDate);
NSLog(@"Request Duration: %f", [transaction.duration doubleValue]);
NSLog(@"Bytes Sent: %lld", transaction.countOfRequestBodyBytesSent);
NSLog(@"Bytes Received: %lld", transaction.countOfResponseBodyBytesReceived);
NSLog(@"Redirection Count: %lu", (unsigned long)transaction.redirectCount);
NSLog(@"Request Method: %@", transaction.request.HTTPMethod);
}
} @end
@interface MyURLProtocol : NSURLProtocol @end @interface MyURLProtocol() @property(nonatomic, strong) NSURLConnection *connection;
@property(atomic, strong) NSMutableData *receivedData; @end @implementation MyURLProtocol + (BOOL)canInitWithRequest:(NSURLRequest *)request {
if ([NSURLProtocol propertyForKey:kCustomURLProtocolKey inRequest:request]) {
return NO;
} #if defined(Debug)
return YES;
#elif defined(Release)
return NO;
#endif
} + (NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request {
return request;
} - (void)startLoading {
NSMutableURLRequest *mutableRequest = [[self request] mutableCopy];
[NSURLProtocol setProperty:@YES forKey:kCustomURLProtocolKey inRequest:mutableRequest];
self.connection = [NSURLConnection connectionWithRequest:mutableRequest delegate:self];
} - (void)stopLoading {
[self.connection cancel];
self.connection = nil;
} #pragma mark - NSURLConnection Delegate #pragma mark - NSURLConnection Delegate
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[[self client] URLProtocol:self didReceiveResponse:response
cacheStoragePolicy:NSURLCacheStorageNotAllowed];
} - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[[self client] URLProtocol:self didLoadData:data]; if (self.receivedData == nil) {
self.receivedData = [[NSMutableData alloc] init];
} [self.receivedData appendData:data];
} -(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSURLRequest *logRequest = [self.request copy];
NSData *data = [self.receivedData copy];
[[self client] URLProtocolDidFinishLoading:self];
self.connection = nil;
#if defined(Debug)
dispatch_async(dispatch_get_main_queue(), ^{
[[LogManager sharedInstance] addNetworkLog:logRequest response:data];
});
#else
dispatch_async(dispatch_get_main_queue(), ^{
[[LogManager sharedInstance] addNetworkLog:logRequest response:data];
});
#endif } - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
[[self client] URLProtocol:self didFailWithError:error];
self.connection = nil;
} @end
iOS网络数据指标收集的更多相关文章
- ios 网络数据下载和JSON解析
ios 网络数据下载和JSON解析 简介 在本文中笔者将要给大家介绍ios中如何利用NSURLConnection从网络上下载数据,如何解析下载下来的JSON数据格式,以及如何显示数据和图片的异步下载 ...
- 浅议iOS网络数据解析
/*------------------------------------ 数据解析: 1.JSON数据 --------------------------------*/ 重点:1.什么是JSO ...
- ios的网络数据下载和json解析
ios的网络数据下载和json解析 简介 在本文中,笔者将要给大家介绍如何使用nsurlconnection 从网上下载数据,以及解析json数据格式,以及如何显示数据和图片的异步下载显示. 涉及的知 ...
- iOS开发——网络实用技术OC篇&网络爬虫-使用青花瓷抓取网络数据
网络爬虫-使用青花瓷抓取网络数据 由于最近在研究网络爬虫相关技术,刚好看到一篇的的搬了过来! 望谅解..... 写本文的契机主要是前段时间有次用青花瓷抓包有一步忘了,在网上查了半天也没找到写的完整的教 ...
- iOS开发——网络使用技术OC篇&网络爬虫-使用正则表达式抓取网络数据
网络爬虫-使用正则表达式抓取网络数据 关于网络数据抓取不仅仅在iOS开发中有,其他开发中也有,也叫网络爬虫,大致分为两种方式实现 1:正则表达 2:利用其他语言的工具包:java/Python 先来看 ...
- iOS—网络实用技术OC篇&网络爬虫-使用java语言抓取网络数据
网络爬虫-使用java语言抓取网络数据 前提:熟悉java语法(能看懂就行) 准备阶段:从网页中获取html代码 实战阶段:将对应的html代码使用java语言解析出来,最后保存到plist文件 上一 ...
- ios网络学习------4 UIWebView的加载本地数据的三种方式
ios网络学习------4 UIWebView的加载本地数据的三种方式 分类: IOS2014-06-27 12:56 959人阅读 评论(0) 收藏 举报 UIWebView是IOS内置的浏览器, ...
- ios网络学习------6 json格式数据的请求处理
ios网络学习------6 json格式数据的请求处理 分类: IOS2014-06-30 20:33 471人阅读 评论(3) 收藏 举报 #import "MainViewContro ...
- iOS - NetRequest 网络数据请求
1.网络请求 1.1 网络通讯三要素 1.IP 地址(主机名): 网络中设备的唯一标示.不易记忆,可以用主机名(域名). 1) IP V4: 0~255.0~255.0~255.0~255 ,共有 2 ...
- iOS开发——网络实用技术OC篇&网络爬虫-使用java语言抓取网络数据
网络爬虫-使用java语言抓取网络数据 前提:熟悉java语法(能看懂就行) 准备阶段:从网页中获取html代码 实战阶段:将对应的html代码使用java语言解析出来,最后保存到plist文件 上一 ...
随机推荐
- MyBatisPlus映射匹配兼容性
字段映射与表名映射 1.当数据库表名tbl_user与实体类名User不一致时:在实体类上添加 :@TableName("tbl_user") package com.itheim ...
- 字符串常见API(charCodeAt\fromCharCode)
1.myStr.charCodeAt(num) 返回指定位置的字符的Unicode(是字符编码的一种模式)编码. 2.String.fromCharCode() String的意思就是不能用自己定义的 ...
- 重打包APK绕过签名校验
这里先提一种针对性校强但简单好理解的办法,纯Java实现,代码大概也就50行不到吧. 还有更强的并且能过各种保护(反调试反HOOK反内存修改等等)的万能方法,不过较复杂,长篇大论的,等有空整理出来再提 ...
- 细节拉满,80 张图带你一步一步推演 slab 内存池的设计与实现
1. 前文回顾 在之前的几篇内存管理系列文章中,笔者带大家从宏观角度完整地梳理了一遍 Linux 内存分配的整个链路,本文的主题依然是内存分配,这一次我们会从微观的角度来探秘一下 Linux 内核中用 ...
- 内网搭建DNS服务器
DNS:Domain Name Service,域名解析服务 监听端口:udp/53,tcp/53 应用程序:bind 根域:. 一级域: 组织域:.com, .org, .net, .mil, .e ...
- [Java SE]javac编译时编码错误
1 问题复现 Information:java: javacTask: 源发行版 8 需要目标发行版 1.8 Information:java: Errors occurred while compi ...
- kali装机 安装输入法 修改国内源
1-先配置国内源官方kali源 vim /etc/apt/sources.list 插入如下源 deb http://mirrors.aliyun.com/kali sana main non-fre ...
- 随手记:Redis 部署到linux上面后,本地无法连接
修改redis的配置文件 redis.conf 1. bind 设置为 0.0.0.0 2. protected-mode 设置为no (也就是关闭保护模式) 3. daemonize 设置 ...
- php对接snmp设备详细讲解
1.Php安装snmp扩展 1.基础环境准备 Php7.2版本 yum -y install php72w-snmp Php7.4版本 yum install net-snmp php-snmp ne ...
- 浅谈php GC(垃圾回收)机制及其与CTF的一点缘分
0x00 侠客日常(一):CTF江湖试剑 众所周知,在php中,当对象被销毁时会自动调用__destruct()方法,同时也要知道,如果程序报错或者抛出异常,则就不会触发该魔术方法. 看题: < ...