cURL

cURL是一个利用URL语法在命令行下工作的文件传输工具,1997年首次发行。它支持文件上传和下载,所以是综合传输工具,但按传统,习惯称cURL为下载工具。cURL还包含了用于程序开发的libcurl。
cURL支持的通信协议有:
FTPFTPSHTTPHTTPSTFTPSFTPGopherSCPTelnetDICTFILELDAP、LDAPS、IMAPPOP3SMTPRTSP

CURL语法格式:curl [option] <URL>

常用命令:####

  • curl <URL>——下载文件写入到标准输出(stdout)
  • curl -o filename <URL>——下载文件写入到文件中。
    eg:curl -o index.html www.example.com/index.html
  • curl -O <URL>——下载文件写入到文件中。
    eg:curl -o index.html www.example.com/icon.png
    文件被保存到本地icon.png文件中
  • curl -O <URL1> -O <URL2>——同时下载多个文件,cURl会尝试重用连接。
  • curl -C -O <URL> ——-C选项可对大文件使用断点续传。
  • curl --limit-rate numerical[G|M|K|B] <URL>——限制传输速率。
    eg:curl --limit-rate 100k www.example.com
  • curl -z day-month-year <URL>——指定时间更新过才会进行下载。
  • curl -u username <URL>——使用用户名登录。
  • curl -c <filename> <URL>——保存cookie文件。
  • curl -b/--cookie <name=data> <URL>——设置cookie。
  • curl -d <key=value> <URL> ——提交表单数据。
  • curl -d @filename ——提交文件中的数据。
    eg: curl -d @data.xml http://www.example.com
  • curl -D filename ——将返回的Header保存为文件。
  • curl -H <header:value>——为HTTP请求设置任意header及值。
    eg: curl -H "Connection:keep-alive" http://www.example.com
  • curl -X/--request method 用户自定义HTTP请求。
    eg: curl -X GET www.example.com
    curl -X POST www.example.com
    curl -X DELETE www.example.com
    // 提交表单数据
    curl -X POST --data "data=xxx" www.example.com
    // 对表单数据编码
    curl -X POST--data-urlencode "date=2016-08-30" www.example.com
  • curl --compressed ——采用压缩方式接收返回数据。
  • curl -L ——允许重定向。
    eg:curl -L www.sina.com 会重定向到 www.sina.com.cn
  • curl -i ——显示http response的头信息,同时显示网页内容。
  • curl -I ——只显示http response的头信息。
  • curl -V——显示一次http通信的整个过程,包括端口连接和http request头信息。
    eg: curl -v www.sina.com
    输出结果如下:
    * Rebuilt URL to: www.sina.com/
    * Trying 121.14.1.190...
    * Connected to www.sina.com (121.14.1.190) port 80 (#0)
    > GET / HTTP/1.1
    > Host: www.sina.com
    > User-Agent: curl/7.43.0
    > Accept: /
    >
    < HTTP/1.1 301 Moved Permanently
    < Server: nginx
    < Date: Sun, 18 Sep 2016 20:43:08 GMT
    < Content-Type: text/html
    < Location: http://www.sina.com.cn/
    < Expires: Sun, 18 Sep 2016 20:45:08 GMT
    < Cache-Control: max-age=120
    < Age: 67
    < Content-Length: 178
    < X-Cache: HIT from ctc.gz.1cf2.39.spool.sina.com.cn
    <
    <html>
    <head><title>301 Moved Permanently</title></head>
    <body bgcolor="white">
  • curl --trace outfile——将http通信过程的详细信息保存到文件中。
  • curl -- from ——实现文件上传表单的功能。
    文件上传的表单是下面这样:
     <form method="POST" enctype='multipart/form-data' action="upload.cgi">
    

<input type=file name=upload>
    <input type=submit name=press value="OK">
  </form>curl上传文件:curl --form upload=@localfilename --form press=OK [URL]```

  • curl -A/--user-agent <User Agent> <URL> ——设置代理。
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容