curl命令学习笔记

博主最近在学习 ES(elastic search) 的过程中,接触到了 curl 命令,并被其简洁而又实用的优点所吸引。因此,博主撰写此博文作为 curl 命令的学习笔记,并希望能对读者有所帮助。本篇博客主要学自阮一峰老师的博客curl网站开发指南

简介

curl 是一个利用URL语法在命令行下工作的文件传输工具。它支持文件上传和下载,所以是综合传输工具,但按传统,习惯称 curl 为下载工具。其语法格式及常见参数含义如下,

语法

curl [option] [url]

常见参数

-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                     进度条显示当前的传送状态

功能

查看网页源码

在curl命令后之间添加网址,就可以看到网页源码。

返回结果

<!DOCTYPE html>
<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body link=#0000cc> 
...

此外,如果我们想将网页保存下来,可以使用参数 -o

curl -o [文件名] www.baidu.com

自动跳转

在日常生活中,我们发现有些网址是自动跳转的,而使用 -L 参数,我们也可以达到同样的目标

curl -L www.sina.com

返回结果 www.sina.com.cn

显示头信息

如果我们想要获取响应的头信息,可以使用参数 -i

curl -i www.baidu.com

返回结果

HTTP/1.1 200 OK
Accept-Ranges: bytes
Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
Connection: Keep-Alive
Content-Length: 2381
Content-Type: text/html
Date: Sun, 16 Jun 2019 12:20:56 GMT
Etag: "588604c4-94d"
Last-Modified: Mon, 23 Jan 2017 13:27:32 GMT
Pragma: no-cache
Server: bfe/1.0.8.18
Set-Cookie: BDORZ=27315; max-age=86400; domain=.baidu.com; path=/

<!DOCTYPE html>
<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body link=#0000cc> <div id=wrapper> <div id=head>
...

-I参数,只显示响应头信息。

显示通信过程

如果你想要了解一次http通信的整个过程,则可以使用 -v 参数。它可以返回端口连接和 http request 头信息

返回结果

* Rebuilt URL to: www.baidu.com/
*   Trying 182.61.200.7...
* TCP_NODELAY set
* Connected to www.baidu.com (182.61.200.7) port 80 (#0)
> GET / HTTP/1.1
> Host: www.baidu.com
> User-Agent: curl/7.54.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Accept-Ranges: bytes
< Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
< Connection: Keep-Alive
< Content-Length: 2381
< Content-Type: text/html
< Date: Sun, 16 Jun 2019 14:38:52 GMT
< Etag: "588604c8-94d"
< Last-Modified: Mon, 23 Jan 2017 13:27:36 GMT
< Pragma: no-cache
< Server: bfe/1.0.8.18
< Set-Cookie: BDORZ=27315; max-age=86400; domain=.baidu.com; path=/
<
<!DOCTYPE html>
<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body link=#0000cc> 
...

而如果你觉得上面的信息还不够,则可以通过 --trance 或者 --trace-ascii 参数查看更详细的通信过程

返回结果

== Info: Rebuilt URL to: www.baidu.com/
== Info:   Trying 182.61.200.7...
== Info: TCP_NODELAY set
== Info: Connected to www.baidu.com (182.61.200.7) port 80 (#0)
=> Send header, 77 bytes (0x4d)
0000: 47 45 54 20 2f 20 48 54 54 50 2f 31 2e 31 0d 0a GET / HTTP/1.1..
0010: 48 6f 73 74 3a 20 77 77 77 2e 62 61 69 64 75 2e Host: www.baidu.
0020: 63 6f 6d 0d 0a 55 73 65 72 2d 41 67 65 6e 74 3a com..User-Agent:
0030: 20 63 75 72 6c 2f 37 2e 35 34 2e 30 0d 0a 41 63  curl/7.54.0..Ac
0040: 63 65 70 74 3a 20 2a 2f 2a 0d 0a 0d 0a          cept: */*....
<= Recv header, 17 bytes (0x11)
0000: 48 54 54 50 2f 31 2e 31 20 32 30 30 20 4f 4b 0d HTTP/1.1 200 OK.
0010: 0a                                              .
<= Recv header, 22 bytes (0x16)
...

发送表单信息

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

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

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

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

此外,如果你的数据没有经过表单编码,还可以通过参数 --data-urlencodecurl 为你编码,

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

HTTP动作

curl默认的HTTP动词是GET,使用 -X 参数可以支持其他动词,例如在 ES 中就会有 DELETE 等操作,

curl -X DELETE www.example.com

文件上传

正如开头所说, curl 命令支持文件上传与下载。假定文件上传的表单是下面这样,

<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]

Referer字段

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

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

User Agent字段

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

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

cookie

你还可以利用 --cookie 参数发送 cookie

curl --cookie "name=xxx" www.example.com

至于具体的cookie的值,你可以从http response头信息的Set-Cookie字段中得到。

-c cookie-file可以保存服务器返回的cookie到文件,-b cookie-file可以使用这个文件作为cookie信息,进行后续的请求。

curl -c cookies http://example.com
curl -b cookies http://example.com

增加头信息

如果你想要自行增加头信息,则可以使用 --header

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

HTTP认证

有时需要 HTTP 认证,则需要用到 --user 参数

curl --user name:password example.com

参考文献

  1. http://www.ruanyifeng.com/blog/2011/09/curl.html
  2. https://www.cnblogs.com/duhuo/p/5695256.html
  3. https://baike.baidu.com/item/curl/10098606?fr=aladdin
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值