curl is a tool to transfer data from or to a server, using one of the supported protocols (DICT, FILE, FTP, FTPS, GOPHER, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMB, SMBS, SMTP, SMTPS, TELNET and TFTP). The command is designed to work without user interaction.
curl offers a busload of useful tricks like proxy support, user authentication, FTP upload, HTTP post, SSL connections, cookies, file transfer resume, Metalink, and more. As you will see below, the number of features will make your head spin!
curl is powered by libcurl for all transfer-related features. See libcurl(3) for details.这里是引用
常用方法总结
- 查看网页源码
$ curl www.sina.com
- 保存网页源码
$ curl -o [文件名] www.sina.com
- 自动跳转
$ curl -L www.sina.com
- 显示http response的头信息
$ curl -i www.sina.com
- 显示一次http通信的整个过程
curl -v www.sina.com
- 查看更详细的通信过程
$ curl --trace output.txt www.sina.com
$ curl --trace-ascii output.txt www.sina.com
- 设置请求状态(curl默认的HTTP动词是GET)
curl -X POST www.example.com
$ curl -X DELETE www.example.com
- 设置请求来源
提供一个referer字段,表示你是从哪里跳转过来的。
$ curl --referer http://www.example.com http://www.example.com
- 增加客户端的设备信息
``` $ curl --user-agent “Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7” www.sina.com`` - 增加头信息
$ curl --header "Content-Type:application/json" http://example.com
- HTTP认证
$ curl --user name:password example.com
- 增加cookie
curl --cookie "name=xxx" www.example.com
-c cookie-file可以保存服务器返回的cookie到文件
curl -c cookie-file http://example.com
-b cookie-file可以使用这个文件作为cookie信息,进行后续的请求。
curl -b cookie-file http://example.com
- 文件上传
$ curl -F 'img_avatar=@/home/petehouston/hello.txt' http://localhost/upload
参数-F(–form)会给请求添加 enctype=“multipart/form-data” 参数。
上传参数img_avatar
路径/home/petehouston/hello.txt
上传到 http://localhost/upload`
阮一峰:http://www.ruanyifeng.com/blog/2011/09/curl.html
文件上传:https://www.gonever.com/post/45