CURL命令模拟Http Get/Post以及带cookies调用接口

在对后端程序进行测试的时候,需要进行模拟连接或者书写测试脚本.

curl是一个很棒的命令.

例如目标网站Url:
127.0.0.1:8080/check_your_status?user=Summer&passwd=12345678
通过Get方法请求:
curl protocol://address:port/url?args
curl http://127.0.0.1:8080/check_your_status?user=Summer&passwd=12345678
通过Post方法请求:
curl -d "args" "protocol://address:port/url"
curl -d "user=Summer&passwd=12345678" "http://127.0.0.1:8080/check_your_status"
这种方法是参数直接在header里面的
如需将输出指定到文件可以通过重定向进行操作.
curl -H "Content-Type:application/json" -X POST --data (json.data) URL
curl -H "Content-Type:application/json" -X POST --data '{"message": "sunshine"}' http://localhost:8000/
这种方法是json数据直接在body里面的

使用Curl命令查看请求响应时间方法
curl -o /dev/null -s -w %{time_namelookup}::%{time_connect}::%{time_starttransfer}::%{time_total}::%{speed_download}"\n" http://www.36nu.com

0.014::0.015::0.018::0.019::1516256.00
编写测试实例:
curl -b “user_trace_token=20150518150621-02994ed9a0fb42d1906a56258e072fc4;LGUID=20150515135257-a33a769c-fac6-11e4-91ce-5254005c3644” http://10.10.26.164:1235/click?v=1&logtype=deliver&position=home_hot-0&orderid=10197777&userid=1942556&positionid=148&url=http%3a%2f%2fwww.lagou.com%2fjobs%2f317000.html%3fsource%3dhome_hot%26i%3dhome_hot-5&fromsite=http%3a%2f%2fwww.lagou.com%2fzhaopin%2fAndroid%3flabelWords%3dlabel%26utm_source%3dAD__baidu_pinzhuan%26utm_medium%3dsem%26utm_campaign%3dSEM&optime=2015-06-15_20:00:00
发现这样还是不可以,url附带的参数取不到。使用-d 参数传递url参数,使用-G 把请求方式配置为GET就OK了,如下:
curl -b “user_trace_token=20150518150621-02994ed9a0fb42d1906a56258e072fc4;LGUID=20150515135257-a33a769c-fac6-11e4-91ce-5254005c3644;LGSID=20150518150621-02994ed9a0fb42d1906a56258e072fc4;LGRID=20150617230732-4ea87972-1580-11e5-9a88-000c29653e90;” -d “v=1&logtype=deliver&position=i_home-1&orderid=10197777&userid=1942556&positionid=148&url=http%3a%2f%2f www.lagou.com%2fjobs%2f317000.html%3fsource%3dhome_hot%26i%3dhome_hot-5&fromsite=http%3a%2f%2fwww.lagou.com%2fzhaopin%2fAndroid%3flabelWords%3dlabel%26utm_source%3dAD__baidu_pinzhuan%26utm_medium%3dsem%26utm_campaign%3dSEM&optime=2015-06-15_20:00:00 ” -G http://10.10.26.164:1235/click
想要获得response返回的cookie怎么办,使用’-c’参数,同时可以使用-b filename用文件方式表示cookie,配合-c使用更方便
可以先用-c 命令生成一个cookie文件作为模板,再修改这个文件作为-b 参数的文件名。
使用如下:
curl -b c1.txt -c c2.txt -d “v=1&_v=j31&a=406405635&t=pageview&_s=1&dr=http%3a%2f%2fwww.sogou.com%2ftuguang&dl=http%3A%2F%2Fwww.lagou.com%2F%3futm_source%3dad_sougou_pingzhuan&ul=zh-cn&de=UTF-8&dt=%E6%8B%89%E5%8B%BE%E7%BD%91-%E6%9C%80%E4%B8%93%E4%B8%9A%E7%9A%84%E4%BA%92%E8%81%94%E7%BD%91%E6%8B%9B%E8%81%98%E5%B9%B3%E5%8F%B0&sd=24-bit&sr=1600×900&vp=1583×291&je=1&fl=18.0%20r0&_u=MACAAAQBK~&jid=&cid=1312768212.1431333683&tid=UA-41268416-1&z=1204746223” -G http://192.168.52.130:1234/collect
生成的c2.txt内容如下:

Netscape HTTP Cookie File

http://curl.haxx.se/docs/http-cookies.html

This file was generated by libcurl! Edit at your own risk.192.168.52.130 FALSE / FALSE 1757574737 user_trace_token 20150914151217-eedd019e-5aaf-11e5-8a69-000c29653e90

192.168.52.130 FALSE / FALSE 1442217595 LGSID 20150914152955-652a13c5-5ab2-11e5-846d-000c29653e90
192.168.52.130 FALSE / FALSE 1442217595 PRE_UTM
192.168.52.130 FALSE / FALSE 1442217595 PRE_HOST www.huxiu.com
192.168.52.130 FALSE / FALSE 1442217595 PRE_SITE http%3A%2F%2Fwww.huxiu.com%2Ftuguang
192.168.52.130 FALSE / FALSE 1442217595 PRE_LAND http%3A%2F%2Fwww.lagou.com%2F%3F
192.168.52.130 FALSE / FALSE 0 LGRID 20150914152955-652a1630-5ab2-11e5-846d-000c29653e90
192.168.52.130 FALSE / FALSE 1757574737 LGUID 20150914151217-eedd0624-5aaf-11e5-8a69-000c29653e90

使用curl命令访问url并模拟cookie
编写测试实例:
curl -b “user_trace_token=20150518150621-02994ed9a0fb42d1906a56258e072fc4;LGUID=20150515135257-a33a769c-fac6-11e4-91ce-5254005c3644” http://10.10.26.164:1235/click?v=1&logtype=deliver&position=home_hot-0&orderid=10197777&userid=1942556&positionid=148&url=http%3a%2f%2fwww.lagou.com%2fjobs%2f317000.html%3fsource%3dhome_hot%26i%3dhome_hot-5&fromsite=http%3a%2f%2fwww.lagou.com%2fzhaopin%2fAndroid%3flabelWords%3dlabel%26utm_source%3dAD__baidu_pinzhuan%26utm_medium%3dsem%26utm_campaign%3dSEM&optime=2015-06-15_20:00:00
发现这样还是不可以,url附带的参数取不到。使用-d 参数传递url参数,使用-G 把请求方式配置为GET就OK了,如下:
curl -b “user_trace_token=20150518150621-02994ed9a0fb42d1906a56258e072fc4;LGUID=20150515135257-a33a769c-fac6-11e4-91ce-5254005c3644;LGSID=20150518150621-02994ed9a0fb42d1906a56258e072fc4;LGRID=20150617230732-4ea87972-1580-11e5-9a88-000c29653e90;” -d “v=1&logtype=deliver&position=i_home-1&orderid=10197777&userid=1942556&positionid=148&url=http%3a%2f%2f www.lagou.com%2fjobs%2f317000.html%3fsource%3dhome_hot%26i%3dhome_hot-5&fromsite=http%3a%2f%2fwww.lagou.com%2fzhaopin%2fAndroid%3flabelWords%3dlabel%26utm_source%3dAD__baidu_pinzhuan%26utm_medium%3dsem%26utm_campaign%3dSEM&optime=2015-06-15_20:00:00 ” -G http://10.10.26.164:1235/click
想要获得response返回的cookie怎么办,使用’-c’参数,同时可以使用-b filename用文件方式表示cookie,配合-c使用更方便
可以先用-c 命令生成一个cookie文件作为模板,再修改这个文件作为-b 参数的文件名。
使用如下:
curl -b c1.txt -c c2.txt -d “v=1&_v=j31&a=406405635&t=pageview&_s=1&dr=http%3a%2f%2fwww.sogou.com%2ftuguang&dl=http%3A%2F%2Fwww.lagou.com%2F%3futm_source%3dad_sougou_pingzhuan&ul=zh-cn&de=UTF-8&dt=%E6%8B%89%E5%8B%BE%E7%BD%91-%E6%9C%80%E4%B8%93%E4%B8%9A%E7%9A%84%E4%BA%92%E8%81%94%E7%BD%91%E6%8B%9B%E8%81%98%E5%B9%B3%E5%8F%B0&sd=24-bit&sr=1600×900&vp=1583×291&je=1&fl=18.0%20r0&_u=MACAAAQBK~&jid=&cid=1312768212.1431333683&tid=UA-41268416-1&z=1204746223” -G http://192.168.52.130:1234/collect
生成的c2.txt内容如下:

Netscape HTTP Cookie File

http://curl.haxx.se/docs/http-cookies.html

This file was generated by libcurl! Edit at your own risk.192.168.52.130 FALSE / FALSE 1757574737 user_trace_token 20150914151217-eedd019e-5aaf-11e5-8a69-000c29653e90

192.168.52.130 FALSE / FALSE 1442217595 LGSID 20150914152955-652a13c5-5ab2-11e5-846d-000c29653e90
192.168.52.130 FALSE / FALSE 1442217595 PRE_UTM
192.168.52.130 FALSE / FALSE 1442217595 PRE_HOST www.huxiu.com
192.168.52.130 FALSE / FALSE 1442217595 PRE_SITE http%3A%2F%2Fwww.huxiu.com%2Ftuguang
192.168.52.130 FALSE / FALSE 1442217595 PRE_LAND http%3A%2F%2Fwww.lagou.com%2F%3F
192.168.52.130 FALSE / FALSE 0 LGRID 20150914152955-652a1630-5ab2-11e5-846d-000c29653e90
192.168.52.130 FALSE / FALSE 1757574737 LGUID 20150914151217-eedd0624-5aaf-11e5-8a69-000c29653e90

-o:把curl 返回的html、js 写到垃圾回收站[ /dev/null]
-s:去掉所有状态
-w:按照后面的格式写出rt
time_namelookup:DNS 解析域名www.36nu.com的时间
time_commect:client和server端建立TCP 连接的时间
time_starttransfer:从client发出请求;到web的server 响应第一个字节的时间
time_total:client发出请求;到web的server发送会所有的相应数据的时间
speed_download:下周速度 单位 byte/s
上面这条命令及返回结果可以这么理解:
0.014: DNS 服务器解析www.36nu.com 的时间单位是s
0.015: client发出请求,到c/s 建立TCP 的时间;里面包括DNS解析的时间
0.018: client发出请求;到s响应发出第一个字节开始的时间;包括前面的2个时间
0.019: client发出请求;到s把响应的数据全部发送给client;并关闭connect的时间
1516256.00 :下载数据的速度
建立TCP连接到server返回client第一个字节的时间:0.018s – 0.015s = 0.003s
server把响应数据发送给client的时间:0.019s – 0.018 = 0.01s

转载于:https://blog.51cto.com/lookingdream/2087142

你可能感兴趣的:(CURL命令模拟Http Get/Post以及带cookies调用接口)