抓包工具Charles的使用 1. 基本使用 抓取网络http请求 2. 抓取https请求 3. 其它功能

iOS开发中抓包是一项必备的技能, 使用Charles可以方便的进行抓包调试. 安装请参考:Charles安装, 或者下载:官网正版.

安装成功后, 打开Charles, 在 Proxy–>Proxy Settings 中设置端口号为8888(可以自定义)
抓包工具Charles的使用
1. 基本使用 抓取网络http请求
2. 抓取https请求
3. 其它功能
抓包工具Charles的使用
1. 基本使用 抓取网络http请求
2. 抓取https请求
3. 其它功能

(1) 打开网络偏好设置, 查看本机ip地址

抓包工具Charles的使用
1. 基本使用 抓取网络http请求
2. 抓取https请求
3. 其它功能

(2) 手机中设置网络代理

设置–>无线局域网–>”i”号—>http代理–>手动
输入ip地址和端口号(8888, 自定义的Charles的端口号)
抓包工具Charles的使用
1. 基本使用 抓取网络http请求
2. 抓取https请求
3. 其它功能

(3) 授权

打开需要抓包的手机APP,初次使用时,Charles会弹出确认对话框,直接点击”Allow”按钮后就可以看到对应的请求数据。
抓包工具Charles的使用
1. 基本使用 抓取网络http请求
2. 抓取https请求
3. 其它功能

(4) 抓包成功

抓包工具Charles的使用
1. 基本使用 抓取网络http请求
2. 抓取https请求
3. 其它功能

温馨提示:抓完包之后,请把手机WiFi中的HTTP代理关闭。不然可能造成手机无法访问网络。

经过上述步骤, 我们可以抓取http请求了, 但是对于https请求, 则会出现这样的错误
抓包工具Charles的使用
1. 基本使用 抓取网络http请求
2. 抓取https请求
3. 其它功能

这里我们需要进一步的配置

2. 抓取https请求

(1) 下载安装证书

选中Charles,在“Help –>“SSL Proxying”—>“Install Charles Root Certificate”会自动打开钥匙串访问窗口:
找到Charles的https证书, 双击 输入管理员密码始终信任证书
抓包工具Charles的使用
1. 基本使用 抓取网络http请求
2. 抓取https请求
3. 其它功能

(2) 手机安装证书

设置好手机代理方式, 然后打开safari, 在地址栏输入:http://charlesproxy.com/getssl。手机会自动跳转安装“Charles Proxy SSL Proxying”描述文件。如下图所示
抓包工具Charles的使用
1. 基本使用 抓取网络http请求
2. 抓取https请求
3. 其它功能
一直点击安装直到安装成功
抓包工具Charles的使用
1. 基本使用 抓取网络http请求
2. 抓取https请求
3. 其它功能
抓包工具Charles的使用
1. 基本使用 抓取网络http请求
2. 抓取https请求
3. 其它功能
抓包工具Charles的使用
1. 基本使用 抓取网络http请求
2. 抓取https请求
3. 其它功能
在设置–>通用–>描述文件与设备管理中可以查看证书:
抓包工具Charles的使用
1. 基本使用 抓取网络http请求
2. 抓取https请求
3. 其它功能
抓包工具Charles的使用
1. 基本使用 抓取网络http请求
2. 抓取https请求
3. 其它功能

(3) 设置Charles工具的https

Proxy–>SSL Proxying Settings…打开对话框
抓包工具Charles的使用
1. 基本使用 抓取网络http请求
2. 抓取https请求
3. 其它功能
抓包工具Charles的使用
1. 基本使用 抓取网络http请求
2. 抓取https请求
3. 其它功能

点击 Add : 在Host中输入*表示匹配所有主机。https默认端口号:443
抓包工具Charles的使用
1. 基本使用 抓取网络http请求
2. 抓取https请求
3. 其它功能
抓包工具Charles的使用
1. 基本使用 抓取网络http请求
2. 抓取https请求
3. 其它功能

坑一: 此时Charles的配置OK, 但是当我们访问https请求的时候, 发现还是不能访问, 这是因为在手机配置的过程中, 缺少了一步, 信任证书.
在 设置–>通用–>关于本机–>证书信任设置 中信任下载的证书.
抓包工具Charles的使用
1. 基本使用 抓取网络http请求
2. 抓取https请求
3. 其它功能
抓包工具Charles的使用
1. 基本使用 抓取网络http请求
2. 抓取https请求
3. 其它功能
这时再访问https请求发现访问成功. 大功告成.
抓包工具Charles的使用
1. 基本使用 抓取网络http请求
2. 抓取https请求
3. 其它功能

3. 其它功能

(1) 弱网模拟

有时候在开发的时候我们想要模拟一下网络慢的情况,可以在Proxy->Throttle Setting,然后选择Enable Throttling,在Throttle Preset下选择网络类型。
抓包工具Charles的使用
1. 基本使用 抓取网络http请求
2. 抓取https请求
3. 其它功能
抓包工具Charles的使用
1. 基本使用 抓取网络http请求
2. 抓取https请求
3. 其它功能

(2) 调试本地URL

在Charles的 Tools–> Map Local…
抓包工具Charles的使用
1. 基本使用 抓取网络http请求
2. 抓取https请求
3. 其它功能
勾选 Enable Map Local
抓包工具Charles的使用
1. 基本使用 抓取网络http请求
2. 抓取https请求
3. 其它功能
点击add添加本地html 文件, 输入请求地址
抓包工具Charles的使用
1. 基本使用 抓取网络http请求
2. 抓取https请求
3. 其它功能
抓包工具Charles的使用
1. 基本使用 抓取网络http请求
2. 抓取https请求
3. 其它功能
配置完成后, 在设置好代理手机的Safari中输入上一步设置的host地址, 结果如下:
抓包工具Charles的使用
1. 基本使用 抓取网络http请求
2. 抓取https请求
3. 其它功能

附录: 本地测试html源码:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <title></title>
</head>
<body>

<div>
    <h1>自定义网页</h1>
<a href="#"> Charles </a>
</div>

</body>
</html>

(3) 其他的妙用, 有待发掘!

参考资料:
Charles破解注册
iOS开发抓包工具之Charles使用
抓包工具Charles的使用心得