curl命令

利用url规则在命令行下工作的文件传输工具

简言之,在linux命令行模式下,可以达到访问网页,下载网页中文件的功能。
可以设置代理、cookies等,网页以html形式显示。

1. 语法

curl [option] [url]

2. 参数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
-H/--header							 设置header(每个header的设置用一个-H)
-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 进度条显示当前的传送状态

3. 用法示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
1. 访问网页,以html形式显示
curl http://www.baidu.com
2. 保存网页到文件
curl http://www.baidu.com >> baidu.html
curl -o baidu.html http://www.baidu.com
3. 下载网页z中的文件
curl -o b.jpg http://www.baidu.com/a.jpg
curl -O http://www.baidu.com/a.jpg
4. 测试网页返回值
curl -o /dev/null -s -w ${http_code} http://www.baidu.com
5. 指定代理服务器
curl -x 192.168.100.100:1080 http://www.baidu.com
6. 上传文件
7. 显示抓取错误
curl -f http://www.baidu.com
8. 设置header
curl -H 'Accept-Language:es' http://www.baidu.com
9. 发送get请求
curl http://localhost/test/adduser?id=100&name=test
10. 发送post请求
写在header
curl -d "id=100&name=test" http://localhost/test/adduser
body形式
curl -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '{"id":100}' http://localhost/test/adduser