linux下curl的用法

  • 2019 年 10 月 4 日
  • 筆記

参考:http://www.aiezu.com/system/linux/linux_curl_syntax.html

curl [options] [URL…]

curl的常用选项:

            --user-agent <string> 设置用户代理发送给服务器              -basic 使用HTTP基本认证              --tcp-nodelay 使用TCP_NODELAY选项              --referer <URL> 来源网址              --cacert <file> CA证书  (SSL)              --compressed 要求返回是压缩的格式              --header <line>自定义头信息传递给服务器              -I     只显示响应报文首部信息              --limit-rate <rate> 设置传输速度              -u/--user <user[:password]>设置服务器的用户和密码              -0/--http1.0 使用HTTP 1.0              -x 接代理服务器的IP 表示使用这个代理IP去请求其他的网页              -s 静默模式,不显示返回的一大堆页面内容              -d 带参数请求。这样就可以post用户名和密码之类的参数了,模拟登录非常有用。              -c 接文件名,表示将curl时候的服务器返回的cookie存到本地文件中              -b 接cookie文件路径, 表示请求的时候将cookie文件中的信息带上              -L 表示如果在response header中如果有location的话就直接转向到location的地址(redirect地址)                  (HTTP/HTTPS)追随http响应头“Location:”定向到跳转后的页面,(在http响应码为3XX时使用,如301跳转、302跳转)            用法:curl [options] [URL...]                例:curl -e www.baidu.com/index.html 192.168.2.11/index.html                get方式提交数据:                  curl -G -d "name=value&name2=value2" http://www.baidu.com                post方式提交数据:                  curl -d "name=value&name2=value2" http://www.baidu.com #post数据                  curl -d a=b&c=d&txt@/tmp/txt http://www.baidu.com  #post文件                表单方式提交数据:                  curl -F file=@/tmp/me.txt http://www.aiezu.com                #将http header保存到文件                  curl -D /tmp/header http://www.aiezu.com                # 查看是否跳转正常                  curl -I "http://pc.demo.com/" -L            例如:负载均衡中检测服务器存活命令          curl -I -s  http://172.16.20.13|head -1|awk -F " " '{print $2}'            检测网址访问情况:          curl -I -s 'http://api.demo.com/User/Register'|sed -n '1p'|grep '200 OK'            # post提交参数检测是否可以网站登录          if [ ! `curl -s -d 'UserName=xxxxxx&&UserPass=xxxxxx' http://www.demo.com/User/Login |wc -l` -eq 0 ] ; then            echo -e "账号出现无法登陆,请关注"          fi