文章目录

  • 1. 命令简述
  • 2. 基本语法
  • 3. 基础使用
  • 3.1 网页内容保存
  • 3.2 网页响应码
  • 3.3 设置代理
  • 3.4 cookie
  • 3.5 保存header信息
  • 3.6 模仿浏览器
  • 3.7 文件下载
  • 3.8 不输出错误和进度信息
  • 3.9 断点续传
  • 3.10 上传文件
  • 3.11 指定客户端头部
  • 3.12 发送POST请求数据体
  • 3.13 数据进行URL编码
  • 3.14 指定HTTP请求头部


1. 命令简述

curl是Linux下常用的命令行工具,常用来请求Web服务器,原意为:Client URL

2. 基本语法

$ curl [options...] <url>

3. 基础使用

[root@localhost ~]# curl "http://www.baidu.com"
<!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> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=百度一下 class="bg s_btn"></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>新闻</a> <a href=http://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>地图</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>视频</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>贴吧</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>登录</a> </noscript> <script>document.write('<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">登录</a>');</script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">更多产品</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>关于百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>©2017 Baidu <a href=http://www.baidu.com/duty/>使用百度前必读</a>  <a href=http://jianyi.baidu.com/ class=cp-feedback>意见反馈</a> 京ICP证030173号  <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>

3.1 网页内容保存

# 使用重定向保存
$ curl "http://www.baidu.com" > ./aaa.html

# 使用参数-o保存网页内容
$ curl -o bbb.html "http://www.baidu.com"

# 使用参数-O保存网页内某个文件
$ curl -O "http://www.baidu.com/hello.sh"

3.2 网页响应码

$ curl -o /dev/null -s -w%{http_code} "http://www.baidu.com"
200

3.3 设置代理

$ curl -x 192.168.1.1:8080 "http://www.baidu.com"

3.4 cookie

某些网页需要使用cookie记录session信息,对于浏览器,可以轻易处理cookie信息,curl目前也支持

# 使用参数-c保存http请求内response的cookie信息
$ curl -c cookie.txt "http://www.baidu.com" -v 						# 使用-v参数展示头部信息,可以查看到具体cookie信息
< Set-Cookie: BDORZ=27315; max-age=86400; domain=.baidu.com; path=/
[root@localhost ~]# cat cookie.txt 
# Netscape HTTP Cookie File
# http://curl.haxx.se/docs/http-cookies.html
# This file was generated by libcurl! Edit at your own risk.

.baidu.com      TRUE    /       FALSE   1669973736      BDORZ   27315

# 使用保存下来的cookie信息重新发起新的请求
# 很多网页都是通过监视cookie信息来判断是否合规访问,因此需要使用保存的cookie信息
$ curl -b cookie.txt "http://www.baidu.com" -v
< Set-Cookie: BDORZ=27315; max-age=86400; domain=.baidu.com; path=/

3.5 保存header信息

$ curl -D header.txt "http://www.baidu.com"
$ cat header.txt 
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: Thu, 01 Dec 2022 09:37:37 GMT
Etag: "588604dd-94d"
Last-Modified: Mon, 23 Jan 2017 13:27:57 GMT
Pragma: no-cache
Server: bfe/1.0.8.18
Set-Cookie: BDORZ=27315; max-age=86400; domain=.baidu.com; path=/

3.6 模仿浏览器

# 很多网页指定必须使用浏览器进行访问,使用参数-A可以模仿浏览器UA
$ curl -A "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.0)" "http://www.baidu.com" -v
> User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.0)

# 伪造盗链 - referer修改
# 很多服务器会检查http访问的referer从而控制访问,使用参数-e可以设置referer
$ curl -e "http://www.aaa.com" "http://www.baidu.com" -v
> Referer: http://www.aaa.com

3.7 文件下载

#下载文件,等效于wget-o/O
# 使用参数-o下载网页内内容(可以使用域名访问到具体文件完成下载),参数-O下载网页内某个文件
$ curl -o aaa.jpg "http://www.baidu.com/aaa.jpg"
$ curl -O "http://www.baidu.com/aaa.jpg"

# 循环下载
$ curl -o \#1\_\#2.JPG "http:///www.baidu.com/{hello,bb}/dodo[1-5].JPG"
$ ll | grep -E "hello|bb"
-rw-r--r--  1 root   root       162 Dec  1 18:12 bb_1.JPG
-rw-r--r--  1 root   root       162 Dec  1 18:12 bb_2.JPG
-rw-r--r--  1 root   root       162 Dec  1 18:12 bb_3.JPG
-rw-r--r--  1 root   root       162 Dec  1 18:12 bb_4.JPG
-rw-r--r--  1 root   root       162 Dec  1 18:12 bb_5.JPG
-rw-r--r--  1 root   root       162 Dec  1 18:12 hello_1.JPG
-rw-r--r--  1 root   root       162 Dec  1 18:12 hello_2.JPG
-rw-r--r--  1 root   root       162 Dec  1 18:12 hello_3.JPG
-rw-r--r--  1 root   root       162 Dec  1 18:12 hello_4.JPG
-rw-r--r--  1 root   root       162 Dec  1 18:12 hello_5.JPG
# 在hello和bb目录下载的文件,更改名称为hello_1.JPG和bb_1.JPG

# 分块下载,使用参数-r可以设置下载大小范围,参数-o指定输出文件
$ curl -r 0-100 -o test1.jpg "http://www.baidu.com/test.jpg"
$ curl -r 100-200 -o test1.jpg "http://www.baidu.com/test.jpg"
$ cat test* > test.jpg

# 通过ftp下载文件,使用参数-u指定账户和密码
$ curl -O -u admin:admin "ftp://www.test.com/test.txt"
$ curl -O "ftp://admin:admin@www.test.com/test.txt"

# 显示下载进度条,使用参数-#
$ curl -# -o test.html "http://www.baidu.com"
######################################################################## 100.0%

3.8 不输出错误和进度信息

# 使用参数-s不输出错误和进度信息,参数-S指定只输出错误信息
$ curl -s -o test.html "http://www.baidu.com"

3.9 断点续传

# 断点续传,使用参数-C
$ curl -C -o test.html "http://www.baidu.com"

3.10 上传文件

# 上传文件,使用参数-T
$ curl -T aaa.txt -u admin:admin "ftp://www.test.com/txt/"

3.11 指定客户端头部

# 指定客户端头部使用参数-A
$ curl -A -v "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36" "http://www.baidu.com"

3.12 发送POST请求数据体

# 发送POST请求数据体
$ curl -d "login=admin&password=123" -X POST "http://www.baidu.com"
# 使用参数-d后,HTTP请求会自动加上标题头部Content-Type: application/x-www-form-urlencoded,并且自动使用POST方法,因此可以省略-X POST
$ cat data.txt 
login=admin&password=123
$ curl -d "@data.txt" "http://www.baidu.com"

3.13 数据进行URL编码

# 数据进行URL编码
$ curl --data-urlencode "comment=hello world" "http://www.baidu.com"

3.14 指定HTTP请求头部

# 指定HTTP请求头部使用参数-H,-d发送JSON数据
$ curl -d '{"login": "emma", "pass": "123"}' -H "Content-Type: application/json" "http://www.baidu.com"

# 参数-i显示Response头部信息,并打印源码,参数-I显示Response头部信息,不打印源码
# 参数-k跳过SSL检测
# 参数-L指定HTTP请求跟随服务器重定向