0%

CURL工具学习

CURL是什么

CURL (Command Line URL viewer)是一种命令行工具,作用是发出网络请求,然后得到和提取数据,显示在“标准输出”(stdout)上面。

查看网页源码

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

1
curl iisheng.cn

1
2
3
4
5
6
7
8
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<h1>301 Moved Permanently</h1>
<p>The requested resource has been assigned a new permanent URI.</p>
<hr/>Powered by Tengine</body>
</html>

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

1
curl -o fileName iisheng.cn

自动跳转

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

1
curl -L iisheng.cn

键入上面命令,就会直接跳转到https://iisheng.cn

显示头信息

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

1
curl -i iisheng.cn

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
HTTP/1.1 301 Moved Permanently
Server: Tengine
Date: Sat, 13 Apr 2019 08:55:26 GMT
Content-Type: text/html
Content-Length: 278
Connection: keep-alive
Location: https://iisheng.cn/
Via: kunlun8.cn314[,0]
Timing-Allow-Origin: *
EagleId: ca6cf99c15551457264052744e

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<h1>301 Moved Permanently</h1>
<p>The requested resource has been assigned a new permanent URI.</p>
<hr/>Powered by Tengine</body>
</html>

显示通信过程

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

1
curl -v iisheng.cn

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
* Rebuilt URL to: iisheng.cn/
* Trying 202.108.249.197...
* Connected to iisheng.cn (202.108.249.197) port 80 (#0)
> GET / HTTP/1.1
> Host: iisheng.cn
> User-Agent: curl/7.47.0
> Accept: */*
>
< HTTP/1.1 301 Moved Permanently
< Server: Tengine
< Date: Sat, 13 Apr 2019 08:57:09 GMT
< Content-Type: text/html
< Content-Length: 278
< Connection: keep-alive
< Location: https://iisheng.cn/
< Via: kunlun8.cn314[,0]
< Timing-Allow-Origin: *
< EagleId: ca6cf99c15551458295975589e
<
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<h1>301 Moved Permanently</h1>
<p>The requested resource has been assigned a new permanent URI.</p>
<hr/>Powered by Tengine</body>
</html>
* Connection #0 to host iisheng.cn left intact

如果你觉得上面的信息还不够,可以使用

1
curl --trace output.txt

运行后,打开output.txt 查看

发送表单信息

GET请求

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

POST请求

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

HTTP 动词

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

1
curl -X POST iisheng.cn

1
curl -X PUT iisheng.cn

文件上传

1
curl -T file.txt url

Referer

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

User Agent

1
curl --user-agent "[User Agent]" [URL]
1
curl --cookie "name=xxx" www.example.com

增加头信息

1
curl --header "Content-Type:application/json" http://example.com
iisheng wechat
微信扫码关注 Coder阿胜