curl命令 是一个利用URL规定在命令行下工作的文件传输工具。它反对文件的上传和下载,所以是综合传输工具,但按传统,习惯称curl为下载工具。作为一款强力工具,curl反对包含HTTP、HTTPS、ftp等泛滥协定,还反对POST、cookies、认证、从指定偏移处下载局部文件、用户代理字符串、限速、文件大小、进度条等特色。

命令语法

> curl (选项)(参数)

命令选项

-A/--user-agent <string>              设置用户代理发送给服务器-b/--cookie <name=string/file>    cookie字符串或文件读取地位-c/--cookie-jar <file>                    操作完结后把cookie写入到这个文件中-C/--continue-at <offset>            断点续转-D/--dump-header <file>              把header信息写入到该文件中-e/--referer                                  起源网址-f/--fail                                          连贯失败时不显示http谬误-o/--output                                  把输入写到该文件中-O/--remote-name                      把输入写到该文件中,保留近程文件的文件名-r/--range <range>                      检索来自HTTP/1.1或FTP服务器字节范畴-s/--silent                                    静音模式。不输入任何货色-T/--upload-file <file>                  上传文件-u/--user <user[:password]>      设置服务器的用户和明码-w/--write-out [format]                什么输入实现后-x/--proxy <host[:port]>              在给定的端口上应用HTTP代理-#/--progress-bar                        进度条显示以后的传送状态

文件下载

  • curl命令能够用来执行下载、发送各种HTTP申请,指定HTTP头部等操作
  • curl是将下载文件输入到stdout,将进度信息输入到stderr,不显示进度信息应用--silent选项。
> curl https://rumenz.com --silent

下载文件到指定的文件小写-o,大写 -O 保留文件和它的原始文件名

> curl https://rumenz.com/1.html -o 2.html
大写 -O 保留文件和它的原始文件名
> curl https://rumenz.com/1.html -O
--progress显示进度条
> curl https://rumenz.com/2.html -o 2.html --progress

断点续传

> curl -O -u 'rumenz':'test' ftp://rumenz.com/jdk.tar.gz
而后你的连贯忽然断开,你能够用以下命令持续下载
> curl -C -  -O  -u 'rumenz':'test' ftp://rumenz.com/jdk.tar.gz
留神断点续传的参数是-C, 要主动续传的话要应用 -C -, 否则须要手工指定断点的字节地位.

伪造申请起源

> curl -e https://json.im https://rumenz.com
参照页是位于HTTP头部中的一个字符串,用来示意用户是从哪个页面达到以后页面的,如果用户点击网页A中的某个连贯,那么用户就会跳转到B网页,网页B头部的参照页字符串就蕴含网页A的URL。也能够应用--referer选项指定参照页字符串.
-H参数能够通过间接增加标头 Referer,达到同样成果
> curl -H 'Referer: https://json.im' https://rumenz.com

设置申请header

> curl -H "Host:rumenz.com" -H "accept-language:zh-cn" URL

curl的带宽管制

> curl --limit-rate 200k  https://rumenz.com/1.html

用curl进行认证

应用curl选项 -u 能够实现HTTP或者FTP的认证,能够指定明码,也能够不指定明码在后续操作中输出明码:
> curl -u user:pwd https://rumenz.com> curl -u user https://rumenz.com

只打印响应头

> curl -I https://rumenz.comHTTP/1.1 200 OKServer: openresty/1.19.3.1Date: Wed, 02 Jun 2021 13:37:41 GMTContent-Type: text/html; charset=utf-8Connection: keep-alive

应用curl模仿get申请

间接显示网页内容
> curl https://json.im/1.txt123456
显示申请头和网页内容
> curl -i https://json.im/1.txtHTTP/1.1 200 OKServer: openrestyDate: Wed, 02 Jun 2021 14:02:30 GMTContent-Type: text/plainContent-Length: 8Last-Modified: Wed, 02 Jun 2021 14:00:57 GMTConnection: keep-aliveETag: "60b78f19-8"Accept-Ranges: bytes123456
间接显示网页内容
> curl -l https://json.im/1.txt
显示get申请全过程解析
> curl -v https://json.im/1.txt

·

应用curl模仿post申请

> curl -d "param1=value1&param2=value2" https://json.im/login> curl -d'login=rumenz&password=123' -X POST https://json.im/login> curl -d 'login=rumenz' -d 'password=123' -X POST  https://json.im/login
--data-urlencode 参数等同于-d,发送 POST申请的数据体,区别在于会主动将发送的数据进行 URL 编码.
> curl --data-urlencode 'comment=hello world' https://json.im/login

上传文本文件

> curl -d '@data.txt' https://json.im/upload

post json格局的数据

> curl -l -H 'Content-type: application/json' -X POST -d '{"rumenz":"123"}' https://json.im/123.json

向服务器发送 Cookie

> curl https://json.im --cookie "user=rumenz&pass=123456"

Cookie写入到一个文件

> curl -c cookies.txt https://json.im

上传二进制文件

-F 参数用来向服务器上传二进制文件。
> curl -F "file=@123.png" https://json.im/uploadfile

下面命令会给 HTTP 申请加上标头 Content-Type: multipart/form-data ,而后将文件123..png作为file字段上传。

-F 参数能够指定MIME 类型。

> curl -F 'file=@123.png;type=image/png'  https://json.im/uploadfile

下面命令指定 MIME 类型为image/png,否则 curl 会把 MIME 类型设为 application/octet-stream。

-F参数也能够指定文件名。

> curl -F 'file=@123.png;filename=rumenz.png' https://json.im/uploadfile 
下面命令中,原始文件名为123.png,然而服务器接管到的文件名为rumenz.png

申请追随服务器的重定向

-L参数会让 HTTP 申请追随服务器的重定向。curl 默认不追随重定向。
> curl -L -d 'rumenz=123' https://json.im/

调试参数

-v 参数输入通信的整个过程,用于调试。
> curl -v https://json.im/1.txt*   Trying 150.109.147.28...* TCP_NODELAY set* Connected to json.im (150.109.147.28) port 443 (#0)* ALPN, offering h2* ALPN, offering http/1.1* Cipher selection: ALL:!EXPORT:!EXPORT40:!EXPORT56:!aNULL:!LOW:!RC4:@STRENGTH* successfully set certificate verify locations:*   CAfile: /etc/ssl/cert.pem  CApath: none* TLSv1.2 (OUT), TLS handshake, Client hello (1):* TLSv1.2 (IN), TLS handshake, Server hello (2):* TLSv1.2 (IN), TLS handshake, Certificate (11):* TLSv1.2 (IN), TLS handshake, Server key exchange (12):* TLSv1.2 (IN), TLS handshake, Server finished (14):* TLSv1.2 (OUT), TLS handshake, Client key exchange (16):* TLSv1.2 (OUT), TLS change cipher, Client hello (1):* TLSv1.2 (OUT), TLS handshake, Finished (20):* TLSv1.2 (IN), TLS change cipher, Client hello (1):* TLSv1.2 (IN), TLS handshake, Finished (20):* SSL connection using TLSv1.2 / ECDHE-RSA-AES128-GCM-SHA256* ALPN, server accepted to use http/1.1* Server certificate:*  subject: CN=json.im*  start date: Apr 27 14:50:23 2021 GMT*  expire date: Jul 26 14:50:23 2021 GMT*  subjectAltName: host "json.im" matched cert's "json.im"*  issuer: C=US; O=Let's Encrypt; CN=R3*  SSL certificate verify ok.> GET /1.txt HTTP/1.1> Host: json.im> User-Agent: curl/7.54.0> Accept: */*>< HTTP/1.1 200 OK< Server: openresty< Date: Wed, 02 Jun 2021 14:31:36 GMT< Content-Type: text/plain< Content-Length: 8< Last-Modified: Wed, 02 Jun 2021 14:00:57 GMT< Connection: keep-alive< ETag: "60b78f19-8"< Accept-Ranges: bytes<123456* Connection #0 to host json.im left intact
--trace参数也能够用于调试,还会输入原始的二进制数据。
> curl --trace - https://json.im

原文链接:https://rumenz.com/rumenbiji/...
微信公众号:入门小站