Curl还可以这么用

man curl 是这么描述的curl  is  a  tool  to transfer data from or to a server.

简单说就是一个文件传输(上传、下载)工具。

很多同学会发现当去curl –help的时候会吐出来好几屏参数,不单单是英语的问题(如果是英文的问题可以移步到linux curl 命令详解,以及实例 – 海底苍鹰(tank)博客翻译的还是很全面),只能说功能很丰富。当然阮一峰的整理对于入门了解也是很推荐的curl网站开发指南- 阮一峰的网络日志

很多时候要跟踪一些很紧急的故障,打开Manual查看确实很让人抓狂。

  • 情形一、快速查看接口在某台服务器上的请求响应时间
curl -x 123.57.156.244:80 -sv -o /dev/null -w "time_connect: %{time_connect}\ntime_starttransfer: %{time_starttransfer}\ntime_total: %{time_total}\n" http://ip.taobao.com/service/getIpInfo.php?ip=115.183.85.158

-v显示一次http通信的全过程,有端口连接和http request header信息
-s是静默模式不输入任何东西,-sv不显示连接的时间,request header还是会显示。

22

  • 情形二、用gzip压缩和不压缩数据的返回大小
curl -sv -o ./curl.log http://ip.taobao.com/service/getIpInfo.php?ip=115.183.85.158 -H 'Accept-Encoding:gzip,defalte'
  • 情形三、请求H5页面
curl -sv http://m.iqiyi.com/v_19rrafgf20.html -H 'User-Agent: iPhone' | more

注意:容易出现的问题

curl -x '47.52.152.236:80' http://i.api.niliu.me/vote?mid=4262740530471968&uid=6438649230&aid=2886&fid=1001

我们会发现没有达到预期的效果,进一步检查会发现参数没有传过去
有两种解决办法
0、url加引号

curl -x '47.52.152.236:80' 'http://i.api.niliu.me/vote?mid=4262740530471968&uid=6438649230&aid=2886&fid=1001'

1、&符合前加反斜杠\

curl -x '47.52.152.236:80' http://i.api.niliu.me/vote?mid=4262740530471968\&uid=6438649230\&aid=2886\&fid=1001

推荐加引号,好理解并且规范。

  • 情形四、传递header信息

传递Referer、UA、Cookie有多种方式,不方便记忆。个人认为统一用-H来自定义header信息就好了,统一复制请求Request Headers中的信息就好了,不要用额外记忆传递Cookie是’Cookie:’, 传递UA是’User-Agent’, 传递Referer是’Referer:’。注意一点, 每一项都需要加-H

curl 'http://energy.tv.weibo.cn/aj/incrspt'
-d 'eid=10258&suid=1537790411&spt=1&send_wb=&send_text10258&follow_uid=&page_type=tvenergy_index_star'
-H 'Referer:http://energy.tv.weibo.cn' 
-H 'Cookie:WEIBOCN_WM=3333_2001; PHPSESSID=e5gqf2plnagjleq547ip7o47o7; TV-G0=b4588dfb84c9fa3672f6d759224e07d6; _T_WM=60850351fae483727b81e311ea34fb01; WEIBOCN_FROM=1110006030; ALF=1537340157; SUBP=0033WrSXqPxfM725Ws9jqgMF55529P9D9WFM0g884TjMTUZSIxD599Gl5JpX5K-hUgL.FoqXe0ncSh.Ee052dJLoIEXLxK-L122LBKnLxKqL1-eLB.2LxK-L1-eLBKnLxK-L12eL1KMLxKBLB.2LB-et; SSOLoginState=1534748164; MLOGIN=1; SCF=AikRb4Tvb8yb7dsbIDHof-_XuJMBchvyavTpt1OtQfvMB9HCOExN3S9oPjq2h-_jpwmWzAoAo5avPwYAnvPe22o.; SUB=_2A252fhZUDeRhGeBK6FoX9CfOyDyIHXVVgLocrDV6PUJbktANLVjykW1NR6gHRIcfFBLfE4fvZ5YzQGUmjSDJzhBK; SUHB=0QSpZza5r5vxWR; M_WEIBOCN_PARAMS=lfid%3D102803_ctg1_5188_-_ctg1_5188%26luicode%3D20000174'
-H 'User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 
Mobile/15A372 Safari/604.1'
-H 'Accept-Encoding: gzip, deflate'
|jq

传递Referer、UA、Cookie的其他方式(简洁,但是不方法记忆,不建议使用)

# 传递Referer
curl --referer 'https://www.google.com' 'http://niliu.me/articles/407.html'
curl -e'https://www.google.com' 'http://niliu.me/articles/407.html'
# 传递UA
curl --user-agent 'iPhone' 'http://niliu.me/articles/407.html'
curl -A 'https://www.google.com' 'http://niliu.me/articles/407.html'
# 传递Cookie
curl --cookie 'SSOLoginState=1534748164;' 'http://niliu.me/articles/407.html'

header传递更多信息的场景

curl 'http://10.211.1.1:8080/portal/pws?t=li' -H 'Host: 10.211.1.1:8080' -H 'Content-Length: 528' 
-H 'Accept: text/plain, */*; q=0.01' -H 'Origin: http://10.211.1.1:8080' 
-H 'X-Requested-With: XMLHttpRequest' 
-H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.162 Safari/537.36' -H 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8' 
-H 'Referer: http://10.211.1.1:8080/portal/customPage/Downloads/index_login.jsp?url=http://10.210.12.253/' 
-H 'Accept-Encoding: gzip, deflate' -H 'Accept-Language: zh-CN,zh;q=0.9,ja;q=0.8,en;q=0.7'
-H 'Cookie: hello1=salmonl' 
--data 'userName=salmonl&userPwd=d34NiMjgwMjgwQA%3D%3D&userurl=&userip=&basip=&language=Chinese&usermac=null&wlannasid=&wlanssid=&entrance=null&loginVerifyCode=&userDynamicPwddd=&customPageId=101&pwdMode=0&portalProxyIP=10.211.1.1&portalProxyPort=50200&dcPwdNeedEncrypt=1&assignIpType=0&appRootUrl=http%3A%2F%2F10.211.1.1%3A8080%2Fportal%2F&manualUrl=http%3A%2F%2Fwww.sina.com.cn&manualUrlEncryptKey=cqy56OhH6aJ5qMPHtteayw%3D%3D&ifEnablePortalEntrance=true&itUrl=http%3A%2F%2Fwww.sina.com.cn&itUrlPortalSharekey=cqy56OhH6aJ5qMPHtteayw%3D%3D' --compressed

未完待续

Curl还可以这么用》上有1条评论

Eugenecoumb进行回复 取消回复

电子邮件地址不会被公开。 必填项已用*标注