curl 命令详解

curl 是一种命令行工具,作用是发出网络请求,然后获取数据,显示在"标准输出"(stdout)上面。它支持多种协议,下面列举其常用功能。

一、查看网页源码

直接在 curl 命令后加上网址,就可以看到网页源码。以网址 www.sina.com为例(选择该网址,主要因为它的网页代码较短)。
curl www.sina.com

$ curl www.sina.com
<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx</center>
</body>
</html>

如果要把这个网页保存下来,可以使用 -o 参数:

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

二、自动跳转

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

$ curl -L www.sina.com
键入上面的命令,结果自动跳转为 www.sina.com.cn。

三、显示头信息

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

$ curl -i www.sina.com
HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Tue, 23 Aug 2016 08:30:16 GMT
Content-Type: text/html
Location: http://www.sina.com.cn/
Expires: Tue, 23 Aug 2016 08:32:16 GMT
Cache-Control: max-age=120
Age: 102
Content-Length: 178
X-Cache: HIT from xd33-83.sina.com.cn

<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx</center>
</body>
</html>

四、显示通信过程

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

$ curl -v www.sina.com
* Rebuilt URL to: www.sina.com/
* Hostname was NOT found in DNS cache
*   Trying 202.108.33.60...
* Connected to www.sina.com (202.108.33.60) port 80 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.35.0
> Host: www.sina.com
> Accept: */*
> 
< HTTP/1.1 301 Moved Permanently
* Server nginx is not blacklisted
< Server: nginx
< Date: Tue, 23 Aug 2016 08:48:14 GMT
< Content-Type: text/html
< Location: http://www.sina.com.cn/
< Expires: Tue, 23 Aug 2016 08:50:14 GMT
< Cache-Control: max-age=120
< Age: 40
< Content-Length: 178
< X-Cache: HIT from xd33-81.sina.com.cn
< 
<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx</center>
</body>
</html>
* Connection #0 to host www.sina.com left intact

如果觉得上面的信息还不够,那么下面的命令可以查看更详细的通信过程。

$ curl --trace output.txt www.sina.com
或者

$ curl --trace-ascii output.txt www.sina.com
运行后,打开 output.txt 文件查看。

五、发送表单信息

发送表单信息有 GET 和 POST 两种方法。
GET 方法

GET 方法相对简单,只要把数据附在网址后面就行。

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

POST 方法

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

$ curl -X POST --data "data=xxx" example.com/form.cgi

表单编码

如果你的数据没有经过表单编码,还可以让 curl 为你编码,参数是 --data-urlencode。

$ curl -X POST--data-urlencode "date=April 1" example.com/form.cgi

六、HTTP动词

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

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

七、User Agent字段

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

Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.84 Safari/537.36
curl 可以这样模拟:

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

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

$ curl --cookie "name=xxx" www.example.com
至于具体的 cookie 的值,可以从 http response 头信息的 Set-Cookie 字段中得到。

九、增加头信息

有时需要在 http request 之中,自行增加一个头信息。–header 参数就可以起到这个作用。

$ curl --header "Content-Type:application/json" http://example.com

十、HTTP认证

有些网域需要 HTTP 认证,这时 curl 需要用到 --user 或者 -u 参数。

$ curl --user name:password example.com

11 、跳过 https证书校验

-k--insecure参数来跳过证书验证

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值