curl 下载命令及请求测试 Web 服务命令详解

文章
林里克斯

CURL 是一个网络数据传输项目,在命令行或脚本中用于传输数据,一般用于下载文件或测试 Web 服务。

实验平台:CentOS 7.7.1908

CURL 支持DICT,文件,FTP,FTPS,Gopher,HTTP,HTTPS,IMAP,IMAPS,LDAP,LDAPS,MQTT,POP3,POP3S,RTMP,RTMPS,RTSP,SCP,SFTP,SMB,SMBS,SMTP,SMTPS,Telnet和TFTP。curl支持SSL证书,HTTP POST,HTTP PUT,FTP上传,基于HTTP表单的上传,代理,HTTP / 2,HTTP / 3,Cookie,用户+密码验证(基本,普通,摘要,CRAM-MD5,NTLM,协商和Kerberos),文件传输恢复,代理隧道等


一、参数详解


1.官网

https://curl.haxx.se/

2.具体参数详解可参考官网,

https://curl.haxx.se/docs/manpage.html

3.参数解释

  • -A

指定客户端的用户代理标头,即 User-Agent

$ curl -A 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Ch100 Safari/537.36' https://www.baidu.com#将 User-Agent 改成 Chrome 浏览器。

$ curl -A '' https://www.baidu.com
#移除 User-Agen t标头。

#也可以通过 -H 参数直接指定标头,更改 User-Agent。
$ curl -H 'User-Agent: php/1.0' https://www.baidu.com
  • -b

用来向服务器发送 Cookie

$ curl -b 'foo=bar' https://www.baidu.com
#生成一个标头 Cookie: foo=bar,向服务器发送一个名为 foo、值为 bar 的 Cookie。

$ curl -b 'foo1=bar;foo2=bar2' https://www.baidu.com
#发送两个 Cookie。

$ curl -b cookies.txt https://www.baidu.com
#读取本地文件cookies.txt,里面是服务器设置的 Cookie(参见-c参数),将其发送到服务器。
  • -c

将服务器设置的 Cookie 写入一个文件。

$ curl -c cookies.txt https://www.baidu.com
上面命令将服务器的 HTTP 回应所设置 Cookie 写入文本文件cookies.txt。
  • -d

用于发送 POST 请求的数据体。

$ curl -d'login=emma&password=123'-X POST https://www.baidu.com/admin
# 或者
$ curl -d 'login=emma' -d 'password=123' -X POST  https://www.baidu.com/admin
使用 -d 参数以后,HTTP 请求会自动加上标头 Content-Type : application/x-www-form-urlencoded。并且会自动将请求转为 POST 方法,因此可以省略 -X POST。

-d 参数可以读取本地文本文件的数据,向服务器发送。

$ curl -d '@data.txt' https://www.baidu.com/admin
上面命令读取data.txt文件的内容,作为数据体向服务器发送。

--data-urlencode

--data-urlencode 参数等同于 -d,发送 POST 请求的数据体,区别在于会自动将发送的数据进行 URL 编码。

$ curl --data-urlencode 'comment=hello world' https://www.baidu.com/login
上面代码中,发送的数据hello world之间有一个空格,需要进行 URL 编码。
  • -e

用来设置 HTTP 的标头 Referer,表示请求的来源。

curl -e 'https://www.baidu.com?q=example' https://www.kjarbo.com
上面命令将 Referer 标头设为 https://www.baidu.com?q=example。

-H 参数可以通过直接添加标头 Referer,达到同样效果。

curl -H 'Referer:https://www.baidu.com?q=example' https://www.kjarbo.com
  • -F

用来向服务器上传二进制文件。

$ curl -F 'file=@photo.png' https://www.baidu.com/profile
上面命令会给 HTTP 请求加上标头 Content-Type: multipart/form-data,然后将文件 photo.png 作为 file 字段上传。

-F 参数可以指定 MIME 类型。

$ curl -F 'file=@photo.png;type=image/png' https://www.baidu.com/profile
上面命令指定 MIME 类型为 image/png,否则 curl 会把 MIME 类型设为application/octet-stream。

-F 参数也可以指定文件名。

$ curl -F 'file=@photo.png;filename=me.png' https://www.baidu.com/profile
上面命令中,原始文件名为photo.png,但是服务器接收到的文件名为me.png。
  • -G

构造 URL 的查询字符串。

$ curl -G -d 'q=kitties' -d 'count=20' https://www.baidu.com/search
#发出一个 GET 请求,实际请求的 URL 为 https://google.com/search?q=kitties&count=20。如果省略 --G,会发出一个 POST 请求。

如果数据需要 URL 编码,可以结合 --data--urlencode 参数。

$ curl -G --data-urlencode 'comment=hello world' https://www.baidu.com
  • -H

添加 HTTP 请求的标头。

$ curl -H 'Accept-Language: en-US' https://www.baidu.com
#添加 HTTP 标头Accept-Language: en-US。

$ curl -H 'Accept-Language: en-US' -H 'Secret-Message: xyzzy' https://www.baidu.com
#添加两个 HTTP 标头。

$ curl -d '{"login": "emma", "pass": "123"}' -H 'Content-Type: application/json' https://google.com/login
添加 HTTP 请求的标头是Content-Type: application/json,然后用-d参数发送 JSON 数据。
  • -i

打印出服务器回应的 HTTP 标头。

$ curl -i https://www.baidu.com
HTTP/1.1 200 OK
Accept-Ranges: bytes
Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
Connection: keep-alive
Content-Length: 2443
Content-Type: text/html
Date: Mon, 12 Oct 2020 13:10:39 GMT
Etag: "58860402-98b"
Last-Modified: Mon, 23 Jan 2017 13:24:18 GMT
Pragma: no-cache
Server: bfe/1.0.8.18
Set-Cookie: BDORZ=27315; max-age=86400; domain=.baidu.com; path=/

<!DOCTYPE html>
<!--STATUS OK--><html>
...
</html>
#收到服务器回应后,先输出服务器回应的标头,然后空一行,再输出网页的内容。
  • -I

向服务器发出 HEAD 请求,然会将服务器返回的 HTTP 标头打印出来。

$ curl -I https://www.baidu.com
HTTP/1.1 200 OK
Accept-Ranges: bytes
Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
Connection: keep-alive
Content-Length: 277
Content-Type: text/html
Date: Mon, 12 Oct 2020 13:10:14 GMT
Etag: "575e1f6d-115"
Last-Modified: Mon, 13 Jun 2016 02:50:21 GMT
Pragma: no-cache
Server: bfe/1.0.8.18
上面命令输出服务器对 HEAD 请求的回应,不会输出网页内容。

--head 参数等同于 -I

$ curl --head blog.kjarbo.com
HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Mon, 12 Oct 2020 12:56:43 GMT
Content-Type: text/html
Content-Length: 162
Connection: keep-alive
Location: https://blog.kjarbo.com/
  • -k

指定跳过 SSL 检测。

$ curl -k https://www.baidu.com
不会检查服务器的 SSL 证书是否正确。输出网页内容
  • -L

HTTP 请求跟随服务器的重定向。curl 默认不跟随重定向。

$ curl www.kjarbo.com
<html>
<head><title>301 Moved Permanently</title></head>
<body>
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx</center>
</body>
</html>

$ curl -L www.kjarbo.com
<!DOCTYPE html>
<!--STATUS OK--><html>
...
</html>
  • --limit-rate

限制 HTTP 请求和回应的带宽,模拟慢网速的环境。

$ curl --limit-rate 200k https://www.baidu.com
#将带宽限制在每秒 200K 字节
  • -o

将服务器的回应保存成文件,等同于 wget 命令。

$ curl -o txt.html https://www.baidu.com
上面命令将 www.baidu.com 保存成 txt.html。
  • -O

将服务器回应保存成文件,并将 URL 的最后部分当作文件名。

$ curl -O https://home.kjarbo.com:8005/DirectoryLister-3.0.0-beta.10.zip
#可以充当 wget 命令
  • -s

不输出错误和进度信息。

$ curl  https://www.baidushangbao.com
curl: (6) Could not resolve host: www.baidushangbao.com; Name or service not known

$ curl -s https://www.baidushangbao.com
#一旦发生错误,不会显示错误信息。不发生错误的话,会正常显示运行结果。
  • -S

指定只输出错误信息

  • -u

设置服务器认证的用户名和密码。

$ curl -u 'admin:12345' https://www.baidu.com/login
设置用户名为bob,密码为12345,然后将其转为 HTTP 标头Authorization: Basic Ym9iOjEyMzQ1。

curl 能够识别 URL 里面的用户名和密码。

$ curl https://admin:12345@www.baidu.com
能够识别 URL 里面的用户名和密码,将其转为上个例子里面的 HTTP 标头。


$ curl -u 'admin' https://www.baidu.com/login
Enter host password for user 'admin':
只设置了用户名,执行后,curl 会提示用户输入密码。
  • -v

输出通信的整个过程,用于调试。

$ curl -v https://www.baidu.com
* About to connect() to www.baidu.com port 443 (#0)
*   Trying 14.215.177.38...
* Connected to www.baidu.com (14.215.177.38) port 443 (#0)
* Initializing NSS with certpath: sql:/etc/pki/nssdb
*   CAfile: /etc/pki/tls/certs/ca-bundle.crt
  CApath: none
* SSL connection using TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
* Server certificate:
*   subject: CN=baidu.com,O="Beijing Baidu Netcom Science Technology Co., Ltd",OU=service operation department,L=beijing,ST=beijing,C=CN
*   start date: Apr 02 07:04:58 2020 GMT
*   expire date: Jul 26 05:31:02 2021 GMT
*   common name: baidu.com
*   issuer: CN=GlobalSign Organization Validation CA - SHA256 - G2,O=GlobalSign nv-sa,C=BE
GET / HTTP/1.1
User-Agent: curl/7.29.0
Host: www.baidu.com
Accept: */*
<!DOCTYPE html>
<!--STATUS OK--><html>
...
</html>

-trace 参数也可以用于调试,还会输出原始的二进制数据。

$ curl -trace https://www.baidu.com
<!DOCTYPE html>
<!--STATUS OK--><html>
...
</html>
  • -x

在给定的端口上使用 HTTP 代理.如果没有指定代理协议,默认为 HTTP。

  • -X

指定 HTTP 请求的方法。

$ curl -X POST https://www.baidu.com
上面命令对 https://www.baidu.com 发出 POST 请求。

3.文献

-a/--append
#上传文件时,附加到目标文件
--anyauth
#可以使用“任何”身份验证方法
--basic
#使用HTTP基本验证
-B/--use-ascii
#使用ASCII文本传输
--data <data> 同 -d
#HTTP POST方式传送数据
--data-ascii <data>
#以ascii的方式post数据
--data-binary <data>
#以二进制的方式post数据
--negotiate
#使用HTTP身份验证
--digest
#使用数字身份验证
--disable-eprt
#禁止使用EPRT或LPRT
--disable-epsv
#禁止使用EPSV
--egd-file <file>
#为随机数据(SSL)设置EGD socket路径
--tcp-nodelay
#使用TCP_NODELAY选项
-E/--cert <cert[:passwd]>
#客户端证书文件和密码 (SSL)
--cert-type <type>
#证书文件类型 (DER/PEM/ENG) (SSL)
--key <key>
#私钥文件名 (SSL)
--key-type <type>
#私钥文件类型 (DER/PEM/ENG) (SSL)
--pass  <pass>
#私钥密码 (SSL)
--engine <eng>
#加密引擎使用 (SSL). "--engine list" for list
--cacert <file>
#CA证书 (SSL)
--capath <directory>
#CA目   (made using c_rehash) to verify peer against (SSL)
--ciphers <list>
#SSL密码
--compressed
#要求返回是压缩的形势 (using deflate or gzip)
--connect-timeout <seconds>
#设置最大请求时间
--create-dirs
#建立本地目录的目录层次结构
--crlf
#上传是把LF转变成CRLF
--ftp-create-dirs
#如果远程目录不存在,创建远程目录
--ftp-method [multicwd/nocwd/singlecwd]
#控制CWD的使用
--ftp-pasv
#使用 PASV/EPSV 代替端口
--ftp-skip-pasv-ip
#使用PASV的时候,忽略该IP地址
--ftp-ssl
#尝试用 SSL/TLS 来进行ftp数据传输
--ftp-ssl-reqd
#要求用 SSL/TLS 来进行ftp数据传输
-form-string <name=string>
#模拟http表单提交数据
-g/--globoff
#禁用网址序列和范围使用{}和[]
-G/--get
#以get的方式来发送数据
-h/--help
#帮助
--header <line>
#自定义头信息传递给服务器
--ignore-content-length
#忽略的HTTP头信息的长度
-j/--junk-session-cookies
#读取文件时忽略session cookie
--interface <interface>
#使用指定网络接口/地址
--krb4 <level>
#使用指定安全级别的krb4
--limit-rate <rate>
#设置传输速度
--local-port<NUM>
#强制使用本地端口号
-m/--max-time <seconds>
#设置最大传输时间
--max-redirs <num>
#设置最大读取的目录数
--max-filesize <bytes>
#设置最大下载的文件总量
-M/--manual
#显示全手动
-n/--netrc
#从netrc文件中读取用户名和密码
--netrc-optional
#使用 .netrc 或者 URL来覆盖-n
--ntlm
#使用 HTTP NTLM 身份验证
-N/--no-buffer
#禁用缓冲输出
--proxy-anyauth
#选择任一代理身份验证方法
--proxy-basic
#在代理上使用基本身份验证
--proxy-digest
#在代理上使用数字身份验证
--proxy-ntlm
#在代理上使用ntlm身份验证
-P/--ftp-port <address>
#使用端口地址,而不是使用PASV
-Q/--quote <cmd>
#文件传输前,发送命令到服务器
--range-file
#读取(SSL)的随机文件
-R/--remote-time
#在本地生成文件时,保留远程文件时间
--retry <num>
#传输出现问题时,重试的次数
--retry-delay <seconds>
#传输出现问题时,设置重试间隔时间
--retry-max-time <seconds>
#传输出现问题时,设置最大重试时间
--socks4 <host[:port]>
#用socks4代理给定主机和端口
--socks5 <host[:port]>
#用socks5代理给定主机和端口
-t/--telnet-option <OPT=val>
#Telnet选项设置
--trace <file>
#对指定文件进行debug
--trace-ascii <file>
#Like --跟踪但没有hex输出
--trace-time
#跟踪/详细输出时,添加时间戳
--url <URL>
#Spet URL to work with
-V/--version
#显示版本信息
-y/--speed-time
#放弃限速所要的时间。默认为30
-Y/--speed-limit
#停止传输速度的限制,速度时间'秒
-z/--time-cond
#传送时间设置
-0/--http1.0
#使用HTTP 1.0
-1/--tlsv1
#使用TLSv1(SSL)
-2/--sslv2
#使用SSLv2的(SSL)
-3/--sslv3
#使用的SSLv3(SSL)
--3p-quote
#like -Q for the source URL for 3rd party transfer
--3p-url
#使用url,进行第三方传送
--3p-user
#使用用户名和密码,进行第三方传送
-4/--ipv4
#使用IP4
-6/--ipv6
#使用IP6

二、常用案例


1.测试网站打开速度

$ curl -o /dev/null -s -w %{time_namelookup}:%{time_connect}:%{time_starttransfer}:%{time_total} http://www.baidu.com
0.004:0.012:0.024:0.024
解析:建立连接:传输:总

2.测试网页的返回值

$ curl -m 10 -o /dev/null -s -w %{http_code} www.baidu.com
200
$ curl -m 10 -o /dev/null -s -w %{http_code} www.kjarbo.com
301
$ curl -m 10 -o /dev/null -s -w %{http_code} www.baidu.com/aklsdjaklsjd
404
#-m 10 最多查询 10s
#-o /dev/null 屏蔽原有输出信息
#-s  静默模式,不输出任何东西
#-w %{http_code} 控制额外输出

-w "HTTP Code: %{http_code}" 
#可追加内容
$ curl -m 10 -o /dev/null -s -w "HTTP Code: %{http_code}" www.baidu.com
HTTP Code: 200

3.下载显示进度条

$ curl -# -O https://home.kjarbo.com:8005/DirectoryLister-3.0.0-beta.10.zip
######################################################################## 100.0%

4.显示抓取错误

$ curl -f https://www.baidu.com/error
curl: (22) The requested URL returned error: 404 Not Found

5.获取出口 IP

$ curl ipinfo.io
{
  "ip": "219.141.24.236",
  "city": "Anshun",
  "region": "Guizhou",
  "country": "CN",
  "loc": "26.2500,105.9333",
  "org": "AS4134 CHINANET-BACKBONE",
  "timezone": "Asia/Shanghai",
  "readme": "https://ipinfo.io/missingauth"
}

$ curl http://ipv6.ident.me/
240e:338:a1e:af40:d866:cd46:3399:7898

$ curl http://ipv4.ident.me/
39.108.114.109

Over~

版权协议须知!

本篇文章来源于 Uambiguous ,如本文章侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意

667 0 2020-10-08


分享:
icon_mrgreen.gificon_neutral.gificon_twisted.gificon_arrow.gificon_eek.gificon_smile.gificon_confused.gificon_cool.gificon_evil.gificon_biggrin.gificon_idea.gificon_redface.gificon_razz.gificon_rolleyes.gificon_wink.gificon_cry.gificon_surprised.gificon_lol.gificon_mad.gificon_sad.gificon_exclaim.gificon_question.gif
博主卡片
林里克斯 博主大人
一个致力于Linux的运维平台
运维时间
搭建这个平台,只为分享及记载自己所遇之事和难题。

现在时间 2024-04-29

今日天气
站点统计
  • 文章总数:240篇
  • 分类总数:29个
  • 评论总数:10条
  • 本站总访问量 216865 次

@奥奥

@Wong arrhenius 牛比

@MakerFace 厉害了!