iOS:根据日志去定位网络请求发生的错误是由于服务端造成的,还是客户端造成的?
一、介绍
在项目开发中,服务端和客户端的协作尤为重要,而连接它们的最重要的环节之一就是网络请求,对于服务端而言,如果这个环节出现了错误,那么安全性就无从谈起,同时对于客户端而言,如果这个模块出现了错误,那么后续的开发就无法继续进行下去。诚然,当网络请求真的出现了问题,快速定位问题的源头就显得迫在眉睫了。本博文提供三种定位问题源头的方法,判断是服务端的原因,还是客户端的原因,避免大家找问题浪费大量的无用功。
二、ATS
一个配置网络请求方式的配置字段,NSAppTranportSecurity下的 Allow Arbitrary Loads,设置为NO,默认所有的网络请求都必须走HTTPS(SSL/TLS)协议;如果设置为YES,即是全局设置,既可以走HTTP协议,也可以走HTPPS(SSL/TLS)协议。
三、问题定位方式(查看网络请求的详细log日志)
第一种方式:配置CFNETWORK_DIAGNOSTICS=1
步骤:打开xcode的项目,找到edit scheme中的run,然后选择Arguments,给Envaironment Variables添加CFNETWORK_DIAGNOSTICS=1,最后运行程序发送网络请求即可。此时在consolelog控制器会给出网络请求详细的log日志文件路径,根据路径找到就OK了。配置如图:
点击按钮测试发送请求,我以我公司的官方来测试(本公司服务端已做了https配置)
// ViewController.m
// RequestDemo
// Created by 夏远全 on 16/12/25.
// Copyright © 2016年 广州市东德网络科技有限公司. All rights reserved.
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor]; //发送请求
UIButton *requestBtn = [[UIButton alloc] initWithFrame:CGRectMake(, , , )];
requestBtn.backgroundColor = [UIColor redColor];
[requestBtn setTitle:@"requestSend" forState:UIControlStateNormal];
[requestBtn setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
[requestBtn addTarget:self action:@selector(requestSend) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:requestBtn];
} //发送请求
-(void)requestSend{ NSURL *URL = [NSURL URLWithString:@"https://www.biaojiepay.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
NSLog(@"complete");
}];
}
@end
打印的显示log日志文件路径如下:
-- ::26.910 RequestDemo[:] CFNetwork diagnostics log file created at: /Users/FanLei/Library/Developer/CoreSimulator/Devices/C4192179-66A1-40C6-B09B-095C1248F68E/data/Containers/Data/Application/FE58F06E-B7BC-428E-82AE-0ED0A567ACD1/Library/Logs/CrashReporter/CFNetwork_com.biaojiepay.RequestDemo_966.nwlrb.log
-- ::27.049 RequestDemo[:] complete
此时按照提供的路径去访问该文件查看,里面记录了网络请求的详细日志信息
Dec :: RequestDemo[] <Notice>: CFNetwork Diagnostics [:] ::26.910 {
LoaderWhatToDo
Request: <CFURL 0x7ff8ea816690 [0x10420ba40]>{string = https://www.biaojiepay.com/, encoding = 134217984, base = (null)}
CachePolicy:
WhatToDo: originload
CreateToNow: .02034s
} [:]
Dec :: RequestDemo[] <Notice>: CFNetwork Diagnostics [:] ::26.912 {
AddCookies Continue: request GET https://www.biaojiepay.com/ HTTP/1.1
HTTPProtocol: Task: e8ca1fd0
} [:]
Dec :: RequestDemo[] <Notice>: CFNetwork Diagnostics [:] ::26.912 {
DiskCookieStorage Construction: Binary{ Disk Cookies: { /Users/FanLei/Library/Developer/CoreSimulator/Devices/C4192179-66A1-40C6-B09B-095C1248F68E/data/Containers/Data/Application/FE58F06E-B7BC-428E-82AE-0ED0A567ACD1/Library/Cookies/com.biaojiepay.RequestDemo.binarycookies, < cookies in domains> clean not writing } }
Accessing: <CFURL 0x7ff8e8ead9f0 [0x10420ba40]>{string = file:///Users/FanLei/Library/Developer/CoreSimulator/Devices/C4192179-66A1-40C6-B09B-095C1248F68E/data/Containers/Data/Application/FE58F06E-B7BC-428E-82AE-0ED0A567ACD1/Library/Cookies/com.biaojiepay.RequestDemo.binarycookies, encoding = 134217984, base = (null)}
Path: /Users/FanLei/Library/Developer/CoreSimulator/Devices/C4192179-66A1-40C6-B09B-095C1248F68E/data/Containers/Data/Application/FE58F06E-B7BC-428E-82AE-0ED0A567ACD1/Library/Cookies/com.biaojiepay.RequestDemo.binarycookies
Read from disk: < cookies in domains>
Dirty: NO
Writing: NO
Policy:
} [:]
Dec :: RequestDemo[] <Notice>: CFNetwork Diagnostics [:] ::26.913 {
DiskCookieStorage Journaling On: Binary{ Disk Cookies: { /Users/FanLei/Library/Developer/CoreSimulator/Devices/C4192179-66A1-40C6-B09B-095C1248F68E/data/Containers/Data/Application/FE58F06E-B7BC-428E-82AE-0ED0A567ACD1/Library/Cookies/com.biaojiepay.RequestDemo.binarycookies, < cookies in domains> clean not writing } }
File: <CFURL 0x7ff8e8caa490 [0x10420ba40]>{string = file:///Users/FanLei/Library/Developer/CoreSimulator/Devices/C4192179-66A1-40C6-B09B-095C1248F68E/data/Containers/Data/Application/FE58F06E-B7BC-428E-82AE-0ED0A567ACD1/Library/Cookies/com.biaojiepay.RequestDemo.binarycookies, encoding = 134217984, base = (null)}
} [:]
Dec :: RequestDemo[] <Notice>: CFNetwork Diagnostics [:] ::26.913 {
Protocol Enqueue: request GET https://www.biaojiepay.com/ HTTP/1.1
Request: <CFURLRequest 0x7ff8ea816700 [0x10420ba40]> {url = https://www.biaojiepay.com/, cs = 0x0}
Message: GET https://www.biaojiepay.com/ HTTP/1.1
} [:]
Dec :: RequestDemo[] <Notice>: CFNetwork Diagnostics [:] ::27.001 {
Peer certificate
Subject Sum: www.biaojiepay.com
Summary: Symantec Basic DV SSL CA - G1
} [:]
Dec :: RequestDemo[] <Notice>: CFNetwork Diagnostics [:] ::27.008 {
Authentication Challenge
Loader: <CFURLRequest 0x7ff8e8c9ba60 [0x10420ba40]> {url = https://www.biaojiepay.com, cs = 0x0}
Challenge: challenge space https://www.biaojiepay.com:443/, ServerTrustEvaluationRequested (Hash c4e968442f77296)
} [:]
Dec :: RequestDemo[] <Notice>: CFNetwork Diagnostics [:] ::27.008 {
Use Credential
Loader: <CFURLRequest 0x7ff8e8c9ba60 [0x10420ba40]> {url = https://www.biaojiepay.com, cs = 0x0}
Credential: null
} [:]
Dec :: RequestDemo[] <Notice>: CFNetwork Diagnostics [:] ::27.009 {
touchConnection
Loader: <CFURLRequest 0x7ff8e8c9ba60 [0x10420ba40]> {url = https://www.biaojiepay.com, cs = 0x0}
Timeout Interval: 60.000 seconds
} [:]
Dec :: RequestDemo[] <Notice>: CFNetwork Diagnostics [:] ::27.046 {
Protocol Received: request GET https://www.biaojiepay.com/ HTTP/1.1
Response: HTTP/1.1 OK
} [:]
Dec :: RequestDemo[] <Notice>: CFNetwork Diagnostics [:] ::27.046 {
touchConnection
Loader: <CFURLRequest 0x7ff8e8c9ba60 [0x10420ba40]> {url = https://www.biaojiepay.com, cs = 0x0}
Timeout Interval: 60.000 seconds
} [:]
Dec :: RequestDemo[] <Notice>: CFNetwork Diagnostics [:] ::27.046 {
destroyReadStream: request GET https://www.biaojiepay.com/ HTTP/1.1
Request: <CFURLRequest 0x7ff8ea816700 [0x10420ba40]> {url = https://www.biaojiepay.com/, cs = 0x0}
sent: <CFNumber 0xb000000000000c53 [0x10420ba40]>{value = +, type = kCFNumberSInt64Type}
received: <CFNumber 0xb00000000000d213 [0x10420ba40]>{value = +, type = kCFNumberSInt64Type}
cell sent: <CFNumber 0xb000000000000003 [0x10420ba40]>{value = +, type = kCFNumberSInt64Type}
cell received: <CFNumber 0xb000000000000003 [0x10420ba40]>{value = +, type = kCFNumberSInt64Type}
} [:]
Dec :: RequestDemo[] <Notice>: CFNetwork Diagnostics [:] ::27.047 {
touchConnection
Loader: <CFURLRequest 0x7ff8e8c9ba60 [0x10420ba40]> {url = https://www.biaojiepay.com, cs = 0x0}
Timeout Interval: 60.000 seconds
} [:]
Dec :: RequestDemo[] <Notice>: CFNetwork Diagnostics [:] ::27.047 {
Response Complete
Request: <CFURLRequest 0x7ff8ea816700 [0x10420ba40]> {url = https://www.biaojiepay.com/, cs = 0x0}
} [:]
Dec :: RequestDemo[] <Notice>: CFNetwork Diagnostics [:] ::27.047 {
Did Finish
Loader: <CFURLRequest 0x7ff8e8c9ba60 [0x10420ba40]> {url = https://www.biaojiepay.com, cs = 0x0}
init to origin load: .021334s
total time: .15739s
total bytes:
} [:]
Dec :: RequestDemo[] <Notice>: CFNetwork Diagnostics [:] ::27.048 {
touchConnection
Loader: <CFURLRequest 0x7ff8e8c9ba60 [0x10420ba40]> {url = https://www.biaojiepay.com, cs = 0x0}
Timeout Interval: 60.000 seconds
} [:]
Dec :: RequestDemo[] <Notice>: CFNetwork Diagnostics [:] ::27.048 {
~HTTPProtocol: nullptr request
Request: null
sent:
received:
cell sent:
cell received:
} [:]
Dec :: RequestDemo[] <Notice>: CFNetwork Diagnostics [:] ::28.914 {
DiskCookieStorage Sync Request
Forced: no
isDirty: no
isWriting: no
File: <CFURL 0x7ff8e8eb0cb0 [0x10420ba40]>{string = file:///Users/FanLei/Library/Developer/CoreSimulator/Devices/C4192179-66A1-40C6-B09B-095C1248F68E/data/Containers/Data/Application/FE58F06E-B7BC-428E-82AE-0ED0A567ACD1/Library/Cookies/com.biaojiepay.RequestDemo.binarycookies, encoding = 134217984, base = (null)}
Journal: yes
Mutations:
} [:]
第二种方式:采用mac自带的命令行查看网络请求日志,即/usr/bin/nscurl --ats-diagnostics --verbose https://www.biaojiepay.com(这个是带上的测试网址)
使用该命令行发送请求测试,我以我公司的官方来测试(本公司服务端已做了https配置,所有验证都会通过Pass)
终端运行结果如下:可以看到全都支持,所以pass通过
Last login: Sun Dec :: on ttys003
FanLeideMacBook-Pro:~ FanLei$ /usr/bin/nscurl --ats-diagnostics --verbose https://www.biaojiepay.com
Starting ATS Diagnostics Configuring ATS Info.plist keys and displaying the result of HTTPS loads to https://www.biaojiepay.com.
A test will "PASS" if URLSession:task:didCompleteWithError: returns a nil error.
================================================================================ Default ATS Secure Connection
---
ATS Default Connection
ATS Dictionary:
{
}
Result : PASS
--- ================================================================================ Allowing Arbitrary Loads ---
Allow All Loads
ATS Dictionary:
{
NSAllowsArbitraryLoads = true;
}
Result : PASS
--- ================================================================================ Configuring TLS exceptions for www.biaojiepay.com ---
TLSv1.
ATS Dictionary:
{
NSExceptionDomains = {
"www.biaojiepay.com" = {
NSExceptionMinimumTLSVersion = "TLSv1.2";
};
};
}
Result : PASS
--- ---
TLSv1.
ATS Dictionary:
{
NSExceptionDomains = {
"www.biaojiepay.com" = {
NSExceptionMinimumTLSVersion = "TLSv1.1";
};
};
}
Result : PASS
--- ---
TLSv1.
ATS Dictionary:
{
NSExceptionDomains = {
"www.biaojiepay.com" = {
NSExceptionMinimumTLSVersion = "TLSv1.0";
};
};
}
Result : PASS
--- ================================================================================ Configuring PFS exceptions for www.biaojiepay.com ---
Disabling Perfect Forward Secrecy
ATS Dictionary:
{
NSExceptionDomains = {
"www.biaojiepay.com" = {
NSExceptionRequiresForwardSecrecy = false;
};
};
}
Result : PASS
--- ================================================================================ Configuring PFS exceptions and allowing insecure HTTP for www.biaojiepay.com ---
Disabling Perfect Forward Secrecy and Allowing Insecure HTTP
ATS Dictionary:
{
NSExceptionDomains = {
"www.biaojiepay.com" = {
NSExceptionAllowsInsecureHTTPLoads = true;
NSExceptionRequiresForwardSecrecy = false;
};
};
}
Result : PASS
--- ================================================================================ Configuring TLS exceptions with PFS disabled for www.biaojiepay.com ---
TLSv1. with PFS disabled
ATS Dictionary:
{
NSExceptionDomains = {
"www.biaojiepay.com" = {
NSExceptionMinimumTLSVersion = "TLSv1.2";
NSExceptionRequiresForwardSecrecy = false;
};
};
}
Result : PASS
--- ---
TLSv1. with PFS disabled
ATS Dictionary:
{
NSExceptionDomains = {
"www.biaojiepay.com" = {
NSExceptionMinimumTLSVersion = "TLSv1.1";
NSExceptionRequiresForwardSecrecy = false;
};
};
}
Result : PASS
--- ---
TLSv1. with PFS disabled
ATS Dictionary:
{
NSExceptionDomains = {
"www.biaojiepay.com" = {
NSExceptionMinimumTLSVersion = "TLSv1.0";
NSExceptionRequiresForwardSecrecy = false;
};
};
}
Result : PASS
--- ================================================================================ Configuring TLS exceptions with PFS disabled and insecure HTTP allowed for www.biaojiepay.com ---
TLSv1. with PFS disabled and insecure HTTP allowed
ATS Dictionary:
{
NSExceptionDomains = {
"www.biaojiepay.com" = {
NSExceptionAllowsInsecureHTTPLoads = true;
NSExceptionMinimumTLSVersion = "TLSv1.2";
NSExceptionRequiresForwardSecrecy = false;
};
};
}
Result : PASS
--- ---
TLSv1. with PFS disabled and insecure HTTP allowed
ATS Dictionary:
{
NSExceptionDomains = {
"www.biaojiepay.com" = {
NSExceptionAllowsInsecureHTTPLoads = true;
NSExceptionMinimumTLSVersion = "TLSv1.1";
NSExceptionRequiresForwardSecrecy = false;
};
};
}
Result : PASS
--- ---
TLSv1. with PFS disabled and insecure HTTP allowed
ATS Dictionary:
{
NSExceptionDomains = {
"www.biaojiepay.com" = {
NSExceptionAllowsInsecureHTTPLoads = true;
NSExceptionMinimumTLSVersion = "TLSv1.0";
NSExceptionRequiresForwardSecrecy = false;
};
};
}
Result : PASS
--- ================================================================================ FanLeideMacBook-Pro:~ FanLei$
第三种方式:如果是服务端的问题,可以采用TLSTool来帮助你定位。(坑:TSLTool不能再Xcode8下编译)
先下载工具编译后,再在终端测试示例:我以我公司的官方来测试,端口443(本公司服务端已做了https配置,所有验证都会通过Pass)
./TLSTool s_client -connect www.biaojiepay.com: GET https://www.biaojiepay.com HTTP/1.1 Host: www.biajiepay.com
iOS:根据日志去定位网络请求发生的错误是由于服务端造成的,还是客户端造成的?的更多相关文章
- iOS中使用block进行网络请求回调
iOS中使用block进行网络请求回调 HttpRequest.h // // HttpRequest.h // UseBlockCallBack // // Created by Michael o ...
- 无法完成你的itunes store 请求发生未知错误50
装上itunes登陆itunes store时遂发现"无法完成您的itunes store的请求,发生未知错误(-50)"跃入眼帘,卸载重装数次还是不见效果,难道是WIN7和itu ...
- 《UNIX网络编程》之多客户连接服务端,可重用套接字对
该网络编程之客户端与服务端程序模板支持: 1. 多客户端同时连接服务端,即服务程序可以同时为多个客户端服务: 2. 服务端支持套接字对重用,即即使处于TIME_WAIT状态,仍可支持服务端重启: 3. ...
- Identity4实现服务端+api资源控制+客户端请求
准备写一些关于Identity4相关的东西,最近也比较对这方面感兴趣.所有做个开篇笔记记录一下,以便督促自己下一个技术方案方向 已经写好的入门级别Identity4的服务+api资源访问控制和简单的客 ...
- 服务端如何安全获取客户端请求IP地址
服务端如何获取客户端请求IP地址,网上代码一搜一大把.其中比较常见有x-forwarded-for.client-ip等请求头,及remote_addr参数,那么为什么会存在这么多获取方式,以及到底怎 ...
- SpringMVC中使用Ajax POST请求以json格式传递参数服务端通过request.getParameter("name")无法获取参数值问题分析
SpringMVC中使用Ajax POST请求以json格式传递参数服务端通过request.getParameter("name")无法获取参数值问题分析 一:问题demo展示 ...
- IOS开发之自动布局显示网络请求内容
在上一篇博客中详细的介绍了IOS开发中的相对布局和绝对布局,随着手机屏幕尺寸的改变,在App开发中为了适应不同尺寸的手机屏幕,用自动布局来完成我们想要实现的功能和效果显得尤为重要.本人更喜欢使用相对布 ...
- IOS 京东相关app 出现“网络请求失败,请检查您的网络设置”的解决办法
问题情况 在IOS系统下,下载安装或者更新新版的京东相关app之后,打开app直接就是“网络请求失败,请检查网络设置”,无论是数据连接还是wifi都试了,都是网络请求失败. 然而打开无线局域网-使用无 ...
- 解决ios开发中不合法的网络请求地址
NSString *const kWebsite = @"http://image.baidu.com/search/index?tn=baiduimage&ipn=r&ct ...
随机推荐
- 利用python的双向队列(Deque)数据结构实现回文检测的算法
#!/usr/bin/env python # -*- coding: utf-8 -*- # learn <<Problem Solving with Algorithms and Da ...
- 13.linux中断处理程序
linux中断处理程序 一.中断处理流程 在linux内核代码中进入entry-armv.S目录: linux统一的入口:__irq svc. 进入了统一的入口之后,程序跳到irq_handler标号 ...
- ASP.Net 在Update Panel局部刷新后 重新绑定JS方法
我们知道Asp.Net中的Update Panel可以完成页面的局部刷新(实质上是Ajax),但是局部刷新完后,此区域的控件上所绑定的JS方法就会失效,因为我们用如下方法来重新绑定. var prm ...
- 12 自定义标签/JSTL标签库/web国际化/java web之设计模式和案例
EL应用 自定义一个标签,实现两个字符串的相加 1回顾 1.1servlet生命周期 init(ServletConfig) service ...
- Delphi基本类型--枚举 子界 集合 数组
[plain] view plain copy <strong>根据枚举定义集合 </strong> TMyColor = (mcBlue, mcRed); TMyColorS ...
- 解决webstorm卡顿问题
webstorm强大的功能就不多做介绍了.但是它的缺点也显而易见:吃内存. 电脑配置稍低一点,运行webstorm就特别容易卡顿,特别是项目比较大的时候,那卡顿得不要不要的. 在我的笔记本8g内存 2 ...
- Leetcode Unique Paths II
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- Ubuntu彻底删除MySQL然后重装MySQL
删除 mysql sudo apt-get autoremove --purge mysql-server-5.0 sudo apt-get remove mysql-server sudo apt- ...
- MongoDB-C#驱动帮助
查增改删 链接字符串 MongoDB超管+(admin) 单独库用户不加 static string mongoR = string.Format("mongodb://{0}(admin) ...
- MySQL查询语句(select)详解(1)
1.查询记录 select*from 表名 [where 条件];eg:select*from students;//查询 students 表中所有记录,所有字段的值都显示出来select fiel ...