curl命令的用法

curl 的一些用法详解,Commad Line URL vi

curl 是一种命令行工具,作用是发出网络请求,然后得到和提取数据,显示在“标准输出”(stdout)上面,他支持多种协议。

在Linux中curl是一个利用URL规则在命令行下工作的文件传输工具,可以说是一款很强大的http命令行工具。它支持文件的上传和下载,是综合传输工具,但按传统,习惯称url为下载工具。

一、查看网页源代码

直接在curl命令后加上网址,可以看到网页源码。

用法:curl URL

$ curl www.sina.com
  <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
  <html><head>
  <title>301 Moved Permanently</title>
  </head><body>
  <h1>Moved Permanently</h1>
  <p>The document has moved <a href="http://www.sina.com.cn/">here</a>.</p>
  </body></html>

这是curl最简单的使用方法,运行上面的命令可以获取 https://www.baidu.com/ 指向的页面,同样,如果这里的 URL 指向的是一个文件或者一幅图,都可以直接下载到本地。

注:由于安装linux的时候很多时候是没有安装桌面的,也意味着没有浏览器,因此这个方法也经常用于测试一台服务器是否可以到达一个网站

如果要把这个文件保存下来,可以使用”-o”参数,相当于使用wget命令。

$ curl -o [文件名] www.sina.com

二、自动跳转

有的网址是自动跳转的。使用-L参数,curl就会跳转到新的网址。

$ curl -L www.sina.com

三、显示头信息

‘-i’,参数可以显示 http response的头信息,连同网页代码一起。

$ curl -i www.sina.com
  HTTP/1.0 301 Moved Permanently
  Date: Sat, 03 Sep 2011 23:44:10 GMT
  Server: Apache/2.0.54 (Unix)
  Location: http://www.sina.com.cn/
  Cache-Control: max-age=3600
  Expires: Sun, 04 Sep 2011 00:44:10 GMT
  Vary: Accept-Encoding
  Content-Length: 231
  Content-Type: text/html; charset=iso-8859-1
  Age: 3239
  X-Cache: HIT from sh201-9.sina.com.cn
  Connection: close
  <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
  <html><head>
  <title>301 Moved Permanently</title>
  </head><body>
  <h1>Moved Permanently</h1>
  <p>The document has moved <a href="http://www.sina.com.cn/">here</a>.</p>
  </body></html>

‘-I‘ 只显示http response的头信息

四、显示通信过程

‘-v’参数可以显示一次http通信的整个过程,包括端口连接和http requests头信息

 $ curl -v www.sina.com
  * About to connect() to www.sina.com port 80 (#0)
  * Trying 61.172.201.195... connected
  * Connected to www.sina.com (61.172.201.195) port 80 (#0)
  > GET / HTTP/1.1
  > User-Agent: curl/7.21.3 (i686-pc-linux-gnu) libcurl/7.21.3 OpenSSL/0.9.8o zlib/1.2.3.4 libidn/1.18
  > Host: www.sina.com
  > Accept: */*
  > 
  * HTTP 1.0, assume close after body
  < HTTP/1.0 301 Moved Permanently
  < Date: Sun, 04 Sep 2011 00:42:39 GMT
  < Server: Apache/2.0.54 (Unix)
  < Location: http://www.sina.com.cn/
  < Cache-Control: max-age=3600
  < Expires: Sun, 04 Sep 2011 01:42:39 GMT
  < Vary: Accept-Encoding
  < Content-Length: 231
  < Content-Type: text/html; charset=iso-8859-1
  < X-Cache: MISS from sh201-19.sina.com.cn
  < Connection: close
  < 
  <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
  <html><head>
  <title>301 Moved Permanently</title>
  </head><body>
  <h1>Moved Permanently</h1>
  <p>The document has moved <a href="http://www.sina.com.cn/">here</a>.</p>
  </body></html>
  * Closing connection #0
$ curl --trace output.txt www.sina.com

五、发送表单信息

发送表单信息有GET和POST两种方法。GET方法相对简单,只要把数据附在网址后面就行。

$ curl example.com/form.cgi?data=xxx

POST方法必须把数据和网址分开,curl就要用到–data参数。

$ curl -X POST --data "data=xxx" example.com/form.cgi     //--data可以写成-d

如果要输出到指定文件可以进行重定向操作

$ curl -H "Content-Type:application/json" -X POST --data '{"message": "sunshine"}' http://localhost:8000/

六、HTTP动词

curl默认的Http动词是GET,使用‘-X’参数可以支持其他动词

$ curl -X POST www.example.com
$ curl -X DELETE www.example.com

七、文件上传

假定文件上传的表单是下面这样的;

  <form method="POST" enctype='multipart/form-data' action="upload.cgi">
    <input type=file name=upload>
    <input type=submit name=press value="OK">
  </form>

八、Referer字段

有时候你需要在http request头信息中,提供一个referer字段,表示你是从哪里跳转过来的

 $ curl --referer http://www.example.com http://www.example.com

九、User Agen字段

这个字段是用来表示客户端的设备信息,服务器有时会根据这个字段,针对不同设备,返回不同格式的网页,比如手机版和桌面版。

iPhone4的User Agen是

  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

curl可以这样模拟

$ curl --user-agent "[User Agent]" [URL]

十、Cookie

使用--cookie参数,可以让curl发送cookie。

$ curl --cookie "name=xxx" www.example.com
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值