一、背景
众所周知,Charles是一款抓包工具,当然是http协议,不支持tcp。(如果你想要抓tcp包,请转而使用wireshark,在讲述websocket的相关技术有梳理过wireshark抓包)
话说回来,charles如果仅仅拿来抓包,就太屈才了。
希望通过我梳理的一系列文章,让你对charles的了解,不仅限于抓包。系列文章包括以下内容:
- Compose 拼接http请求
- Repeat 回放http请求
- 接口映射工具(Map Remote和Map Local)
- Rewrite 重写工具
- DNS spoofing (DNS域名伪装)
- Block List 和 Allow List (黑白名单)
二、拼接http请求
charles 可以像postman等工具一样,发起http请求。
Compose a new request, 输入请求地址,请求方式,content-type

点击"compose",就保存了一个请求模板。

点击"Execute", 发起http请求。
执行结果,见下图:

三、get请求
上面是简单的请求,不含请求参数。
如果要增加请求参数,在compose的URL,可以进行Add。

再次执行请求,在Query String 可以看到上一步新增的参数。

当没有请求参数的时候,则没有Query String这一栏。
四、post请求
除了Method选择POST外,特别需注意Content-Type应该为application/json,默认是application/x-www-form-urlencoded(这种方式适用于Get请求,它是表单请求)

请求体既然是一个Json字符串,输入的位置在JSON Text里。

四、总结
本文举例了http的两种最常见的get请求方式和post请求方式,像postman那样拼接http请求并测试。其他的请求方式大同小异,遇到的时候略微修改即可。
附录
Charles抓包工具系列文章(一)-- Compose 拼接http请求
Charles抓包工具系列文章(二)-- Repeat 回放http请求
Charles抓包工具系列文章(三)-- 接口映射工具(Map Remote和Map Local)
Charles抓包工具系列文章(四)-- Rewrite 重写工具
Charles抓包工具系列文章(五)-- DNS spoofing (DNS域名伪装)
Charles抓包工具系列文章(六)-- Block List 和 Allow List (黑白名单)