linux常用命令之curl

  1.curl是libcurl这个库支持的,默认支持HTTP1.1(也支持1.0)。

  2.curl支持很多的协议。curl supports FTP, FTPS, HTTP, HTTPS, SCP, SFTP, TFTP, TELNET, DICT, LDAP, 
  LDAPS, FILE, POP3, IMAP, SMTP and RTSP at the time of this writing. Wget supports HTTP, HTTPS and FTP.

curl [option] [url]

1.获取页面内容

当我们不加任何选项使用curl时,默认会发送GET请求来获取链接内容到标准输出

curl  localhost:80

2.显示HTTP头

option: -I
curl -I localhost:80
结果
HTTP/1.1 200 OK
Server: nginx/1.17.3
Date: Thu, 12 Sep 2019 10:17:08 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 13 Aug 2019 14:01:07 GMT
Connection: keep-alive
ETag: "5d52c2a3-264"
Accept-Ranges: bytes

3.显示HTTP头和文件内容

option: -i
curl -i localhost:80
结果
HTTP/1.1 200 OK
Server: nginx/1.17.3
Date: Thu, 12 Sep 2019 10:28:53 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 13 Aug 2019 14:01:07 GMT
Connection: keep-alive
ETag: "5d52c2a3-264"
Accept-Ranges: bytes




Welcome to nginx!



Welcome to nginx!

If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.

4.将链接保存到文件

通过输出重定向符号“>”将输出指定到文件中

curl localhost:80 > index.html
option:-o
curl -o index.html localhost:80
option:-O   #URL中要有文件名
curl -O localhost:80/file

5.同时下载多个文件

curl -O localhost:80/file1 -O localhost:80/file2
curl -o file1.html localhost:80/file/1 -o file2.html localhost:80/file/2

6.跟随链接重定向

option -L
curl -L localhost:80

7.自定义header

option -H
curl -H "Host: 118.31.76.144:80" -H "Cookie: token=we8rw9r32ujew8r2jew9823" http://zq.com/html/index.html

8.发送POST请求

option -d
curl -d "username=root&password=123456"  -X POST  http://zq.com/login

-d:用于指定发送的数据,-X:用于指定请求方式
注:在使用-d(默认为POST请求)发送POST请求是可不加-X
如:

curl -d "username=root&password=123456"   http://zq.com/login
curl -d "@data.txt"   http://zq.com/login      #从文件中读取数据

9.发送GET请求

curl -d "data"  -X GET  http://zq.com/api       #强制使用GET请求
curl -d "data"  -G  http://zq.com/api

10.读取 Cookie

option -b   #-b后面可以是 Cookie 字符串,也可以是保存了 Cookie 的文件名
curl -b "token=we8rw9r32ujew8r2jew9823" http://zq.com/html/index.html
curl -b "collie-filename" http://zq.com/html/index.html

11.保存 Cookie

option -c
curl -c "collie-filename" http://zq.com/html/index.html

11.上传

option --form
 curl --form "[email protected]" http://zq.com/resource

常见参数

-A/--user-agent  设置用户代理发送给服务器
-b/--cookie  cookie字符串或文件读取位置
-c/--cookie-jar  操作结束后把cookie写入到这个文件中
-C/--continue-at  断点续转
-D/--dump-header  把header信息写入到该文件中
-e/--referer 来源网址
-f/--fail 连接失败时不显示http错误
-o/--output 把输出写到该文件中
-O/--remote-name 把输出写到该文件中,保留远程文件的文件名
-r/--range  检索来自HTTP/1.1或FTP服务器字节范围
-s/--silent 静音模式。不输出任何东西
-T/--upload-file  上传文件
-u/--user  设置服务器的用户和密码
-w/--write-out [format] 什么输出完成后
-x/--proxy  在给定的端口上使用HTTP代理
-#/--progress-bar 进度条显示当前的传送状态
-z 判断日期
--limit-rate 下载限速

你可能感兴趣的:(linux常用命令之curl)