ab发送cookie和post请求的方法

ab是apache自带的压力测试工具,近期需要压测一个接口,涉及使用post请求,并在其中带cookie。方法总结如下:

1. 发送cookie

方法1

-C key1=value1;key2=value2...

例:

ab -n 1 -C "name=ball;age=99;sex=male" "http://wc.sogou.com/worldcup2018/test.php"

服务端可拿到name, age, sex三个cookie值

方法2

-H "Cookie: key1=value1;key2=value2..."

例:

ab -n 1 -H "Cookie: name=ball;age=36" "http://wc.sogou.com/worldcup2018/test.php"

2. 发送post请求

方法

-T 'application/x-www-form-urlencoded' -p postfile

说明:
1. -T参数指明post数据编码,无需变化。
2. postfile是文件名,里面存放了所要发送的post数据。数据格式如下:

key1=value1&key2=value2...

如果value包含&等特殊符号,则需要对value进行urlencode编码。当然,保险起见,也可以选择在任何情况下都对value进行urlencode。

例1:
postfile内容如下:

age=99&name=ball

发送

ab -n 1 -T 'application/x-www-form-urlencoded' -p post.data "http://wc.sogou.com/worldcup2018/test.php"

服务端收到age, name两个post数据。

例2,一个json的demo
postfile内容如下:

jsondemo=[{"mid":1,"price":10,"guess":3},{"mid":2,"price":20,"guess":3}]&name=ball

发送方式同上。
服务端收到jsondemo为[{“mid”:1,”price”:10,”guess”:3},{“mid”:2,”price”:20,”guess”:3}], json_decode后得到php数组。

postfile也可以是如下形式(将上文中的jsondemo进行urlencode得到):

jsondemo=%5B%7B%22mid%22%3A1%2C%22price%22%3A10%2C%22guess%22%3A3%7D%2C%7B%22mid%22%3A2%2C%22price%22%3A20%2C%22guess%22%3A3%7D%5D&name=ball

猜你喜欢

转载自blog.csdn.net/qmhball/article/details/80513645