linux系列学会使用CURL命令进行URL测试(linux查看curl 请求)

网友投稿 2530 2022-05-30

Linux系列之学会使用CURL命令

一. 查看帮助

1.1 查看命令参数

二. 查询使用手册

2.1 查看详细使用

三. 提取网页

3.1 查看网页源码

3.2 显示头信息

3.3 显示通信过程

3.4 查看议定书

3.5 查看时间

四. 用curl进行认证

4.1 referer设置

4.2 用户代理设置

4.3 用户信息校验

五. 文件操作

5.1 保存文件

5.2 文件下载

5.3 上传文件

六. Cookie设置

6.1 设置cookie

6.2 cookie -b -c

6.3 指定cookie文件

七. curl RESTFul

7.1 RESTFul API

7.2 POST请求

7.3 GET请求

7.4 DELETE请求

7.5 PUT请求

八. CURL参考资料

curl命令是一个利用URL规则在shell终端命令行下工作的文件传输工具;curl命令作为一款强力工具,curl支持包括HTTP、HTTPS、ftp等众多协议,还支持POST、cookies、认证、从指定偏移处下载部分文件、用户代理字符串、限速、文件大小、进度条等特征;做网页处理流程和数据检索自动化。

语法:语法格式:curl [参数] [网址]

一. 查看帮助

1.1 查看命令参数

curl的参数很多,可以用命令curl --help查看:

[root@localhost ~]# curl --help Usage: curl [options...] Options: (H) means HTTP/HTTPS only, (F) means FTP only --anyauth Pick "any" authentication method (H) -a, --append Append to target file when uploading (F/SFTP) --basic Use HTTP Basic Authentication (H) --cacert FILE CA certificate to verify peer against (SSL) --capath DIR CA directory to verify peer against (SSL) -E, --cert CERT[:PASSWD] Client certificate file and password (SSL) --cert-type TYPE Certificate file type (DER/PEM/ENG) (SSL) --ciphers LIST SSL ciphers to use (SSL) --compressed Request compressed response (using deflate or gzip) -K, --config FILE Specify which config file to read --connect-timeout SECONDS Maximum time allowed for connection -C, --continue-at OFFSET Resumed transfer offset -b, --cookie STRING/FILE String or file to read cookies from (H) -c, --cookie-jar FILE Write cookies to this file after operation (H) --create-dirs Create necessary local directory hierarchy --crlf Convert LF to CRLF in upload --crlfile FILE Get a CRL list in PEM format from the given file -d, --data DATA HTTP POST data (H) --data-ascii DATA HTTP POST ASCII data (H) --data-binary DATA HTTP POST binary data (H) --data-urlencode DATA HTTP POST data url encoded (H) --delegation STRING GSS-API delegation permission --digest Use HTTP Digest Authentication (H) --disable-eprt Inhibit using EPRT or LPRT (F) --disable-epsv Inhibit using EPSV (F) -D, --dump-header FILE Write the headers to this file --egd-file FILE EGD socket path for random data (SSL) --engine ENGINGE Crypto engine (SSL). "--engine list" for list -f, --fail Fail silently (no output at all) on HTTP errors (H) -F, --form CONTENT Specify HTTP multipart POST data (H) --form-string STRING Specify HTTP multipart POST data (H) --ftp-account DATA Account data string (F) --ftp-alternative-to-user COMMAND String to replace "USER [name]" (F) --ftp-create-dirs Create the remote dirs if not present (F) --ftp-method [MULTICWD/NOCWD/SINGLECWD] Control CWD usage (F) --ftp-pasv Use PASV/EPSV instead of PORT (F) -P, --ftp-port ADR Use PORT with given address instead of PASV (F) --ftp-skip-pasv-ip Skip the IP address for PASV (F) --ftp-pret Send PRET before PASV (for drftpd) (F) --ftp-ssl-ccc Send CCC after authenticating (F) --ftp-ssl-ccc-mode ACTIVE/PASSIVE Set CCC mode (F) --ftp-ssl-control Require SSL/TLS for ftp login, clear for transfer (F) -G, --get Send the -d data with a HTTP GET (H) -g, --globoff Disable URL sequences and ranges using {} and [] -H, --header LINE Custom header to pass to server (H) -I, --head Show document info only -h, --help This help text --hostpubmd5 MD5 Hex encoded MD5 string of the host public key. (SSH) -0, --http1.0 Use HTTP 1.0 (H) --ignore-content-length Ignore the HTTP Content-Length header -i, --include Include protocol headers in the output (H/F) -k, --insecure Allow connections to SSL sites without certs (H) --interface INTERFACE Specify network interface/address to use -4, --ipv4 Resolve name to IPv4 address -6, --ipv6 Resolve name to IPv6 address -j, --junk-session-cookies Ignore session cookies read from file (H) --keepalive-time SECONDS Interval between keepalive probes --key KEY Private key file name (SSL/SSH) --key-type TYPE Private key file type (DER/PEM/ENG) (SSL) --krb LEVEL Enable Kerberos with specified security level (F) --libcurl FILE Dump libcurl equivalent code of this command line --limit-rate RATE Limit transfer speed to this rate -l, --list-only List only names of an FTP directory (F) --local-port RANGE Force use of these local port numbers -L, --location Follow redirects (H) --location-trusted like --location and send auth to other hosts (H) -M, --manual Display the full manual --mail-from FROM Mail from this address --mail-rcpt TO Mail to this receiver(s) --mail-auth AUTH Originator address of the original email --max-filesize BYTES Maximum file size to download (H/F) --max-redirs NUM Maximum number of redirects allowed (H) -m, --max-time SECONDS Maximum time allowed for the transfer --metalink Process given URLs as metalink XML file --negotiate Use HTTP Negotiate Authentication (H) -n, --netrc Must read .netrc for user name and password --netrc-optional Use either .netrc or URL; overrides -n --netrc-file FILE Set up the netrc filename to use -N, --no-buffer Disable buffering of the output stream --no-keepalive Disable keepalive use on the connection --no-sessionid Disable SSL session-ID reusing (SSL) --noproxy List of hosts which do not use proxy --ntlm Use HTTP NTLM authentication (H) -o, --output FILE Write output to instead of stdout --pass PASS Pass phrase for the private key (SSL/SSH) --post301 Do not switch to GET after following a 301 redirect (H) --post302 Do not switch to GET after following a 302 redirect (H) --post303 Do not switch to GET after following a 303 redirect (H) -#, --progress-bar Display transfer progress as a progress bar --proto PROTOCOLS Enable/disable specified protocols --proto-redir PROTOCOLS Enable/disable specified protocols on redirect -x, --proxy [PROTOCOL://]HOST[:PORT] Use proxy on given port --proxy-anyauth Pick "any" proxy authentication method (H) --proxy-basic Use Basic authentication on the proxy (H) --proxy-digest Use Digest authentication on the proxy (H) --proxy-negotiate Use Negotiate authentication on the proxy (H) --proxy-ntlm Use NTLM authentication on the proxy (H) -U, --proxy-user USER[:PASSWORD] Proxy user and password --proxy1.0 HOST[:PORT] Use HTTP/1.0 proxy on given port -p, --proxytunnel Operate through a HTTP proxy tunnel (using CONNECT) --pubkey KEY Public key file name (SSH) -Q, --quote CMD Send command(s) to server before transfer (F/SFTP) --random-file FILE File for reading random data from (SSL) -r, --range RANGE Retrieve only the bytes within a range --raw Do HTTP "raw", without any transfer decoding (H) -e, --referer Referer URL (H) -J, --remote-header-name Use the header-provided filename (H) -O, --remote-name Write output to a file named as the remote file --remote-name-all Use the remote file name for all URLs -R, --remote-time Set the remote file's time on the local output -X, --request COMMAND Specify request command to use --resolve HOST:PORT:ADDRESS Force resolve of HOST:PORT to ADDRESS --retry NUM Retry request NUM times if transient problems occur --retry-delay SECONDS When retrying, wait this many seconds between each --retry-max-time SECONDS Retry only within this period -S, --show-error Show error. With -s, make curl show errors when they occur -s, --silent Silent mode. Don't output anything --socks4 HOST[:PORT] SOCKS4 proxy on given host + port --socks4a HOST[:PORT] SOCKS4a proxy on given host + port --socks5 HOST[:PORT] SOCKS5 proxy on given host + port --socks5-hostname HOST[:PORT] SOCKS5 proxy, pass host name to proxy --socks5-gssapi-service NAME SOCKS5 proxy service name for gssapi --socks5-gssapi-nec Compatibility with NEC SOCKS5 server -Y, --speed-limit RATE Stop transfers below speed-limit for 'speed-time' secs -y, --speed-time SECONDS Time for trig speed-limit abort. Defaults to 30 --ssl Try SSL/TLS (FTP, IMAP, POP3, SMTP) --ssl-reqd Require SSL/TLS (FTP, IMAP, POP3, SMTP) -2, --sslv2 Use SSLv2 (SSL) -3, --sslv3 Use SSLv3 (SSL) --ssl-allow-beast Allow security flaw to improve interop (SSL) --stderr FILE Where to redirect stderr. - means stdout --tcp-nodelay Use the TCP_NODELAY option -t, --telnet-option OPT=VAL Set telnet option --tftp-blksize VALUE Set TFTP BLKSIZE option (must be >512) -z, --time-cond TIME Transfer based on a time condition -1, --tlsv1 Use => TLSv1 (SSL) --tlsv1.0 Use TLSv1.0 (SSL) --tlsv1.1 Use TLSv1.1 (SSL) --tlsv1.2 Use TLSv1.2 (SSL) --trace FILE Write a debug trace to the given file --trace-ascii FILE Like --trace but without the hex output --trace-time Add time stamps to trace/verbose output --tr-encoding Request compressed transfer encoding (H) -T, --upload-file FILE Transfer FILE to destination --url URL URL to work with -B, --use-ascii Use ASCII/text transfer -u, --user USER[:PASSWORD] Server user and password --tlsuser USER TLS username --tlspassword STRING TLS password --tlsauthtype STRING TLS authentication type (default SRP) -A, --user-agent STRING User-Agent to send to server (H) -v, --verbose Make the operation more talkative -V, --version Show version number and quit -w, --write-out FORMAT What to output after completion --xattr Store metadata in extended file attributes -q If used as the first parameter disables .curlrc

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

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

国内翻译过来的curl文档:curl参数介绍

二. 查询使用手册

2.1 查看详细使用

也可以使用curl --manual查看基本用法例子:

三. 提取网页

3.1 查看网页源码

语法:curl uri

[root@localhost ~]# curl www.baidu.com 百度一下,你就知道

关于百度 About Baidu

©2017 Baidu 使用百度前必读  意见反馈 京ICP证030173号 

1

2

3

4

3.2 显示头信息

curl -i https://www.baidu.com

1

打印信息:

[root@localhost ~]# curl -i https://www.baiud.com curl: (7) Failed connect to www.baiud.com:443; Connection refused [root@localhost ~]# curl -i https://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: 2443 Content-Type: text/html Date: Mon, 13 Jul 2020 07:22:02 GMT Etag: "588603eb-98b" Last-Modified: Mon, 23 Jan 2017 13:23:55 GMT Pragma: no-cache Server: bfe/1.0.8.18 Set-Cookie: BDORZ=27315; max-age=86400; domain=.baidu.com; path=/ ...

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

3.3 显示通信过程

curl -v www.baidu.com

1

curl -v www.baidu.com * About to connect() to www.baidu.com port 80 (#0) * Trying 182.61.200.7... * Connected to www.baidu.com (182.61.200.7) port 80 (#0) > GET / HTTP/1.1 > User-Agent: curl/7.29.0 > Host: www.baidu.com > 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: Mon, 13 Jul 2020 07:24:46 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=/ < ...

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

3.4 查看议定书

[root@localhost ~]# curl --trace-ascii output.txt https://www.baidu.com

1

2

3.5 查看时间

curl --trace-ascii output.txt --trace-time https://www.baidu.com

1

四. 用curl进行认证

4.1 referer设置

referer参数描述了一个网页的来源,可以按F12调试模式,查看:

访问百度时候,加上referer为csdn:

[root@localhost ~]# curl --referer https://blog.csdn.net https://www.baidu.com

1

4.2 用户代理设置

用户代理常被用于兼容ie,有时,您会看到获取带有curl的页面不会返回与使用浏览器获取页面时看到的页面相同的页面,这时候可以设置用户代理:

curl www.baidu.com --user-agent "Mozilla/5.0" curl www.baidu.com -A "Mozilla/5.0"

1

2

4.3 用户信息校验

比如登录百度,可以用-u参数,接着会提示输入密码

[root@localhost ~]# curl -u admin https://www.baidu.net/ Enter host password for user 'admin':

1

2

五. 文件操作

5.1 保存文件

语法:curl -o filename uri

[root@localhost ~]# curl -o baidu.html www.baidu.com % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 2381 100 2381 0 0 22761 0 --:--:-- --:--:-- --:--:-- 22894

1

2

3

4

5

5.2 文件下载

语法:curl uri filename --silent|progress

–silent:不显示进度条

–progress:显示进度条

[root@localhost ~]# curl https://avatar.csdnimg.cn/3/5/E/1_meituantech_1564971608.jpg -omeituan.jpg --progress ######################################################################## 100.0%

1

2

3

5.3 上传文件

语法:$ curl --form upload=@localfilename --form press=OK [URL]

[root@localhost ~]# curl --form upload=@baidu.html --form press=OK url

1

2

六. Cookie设置

6.1 设置cookie

设置cookie,可以用命令--cookie

6.2 cookie -b -c

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

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

1

2

[root@localhost ~]# curl -c "name=cookiename" www.baidu.com

1

2

6.3 指定cookie文件

也可以用–cookie-jar指定cookie文件

[root@localhost ~]# curl www.baidu.com --cookie-jar cookie_file

1

2

指定可下载的最大文件大小,可以用命令–limit-rate

[root@localhost ~]# curl www.baidu.com --limit-rate 50k

1

2

七. curl RESTFul

7.1 RESTFul API

curl支持RESTFul API格式的,所以先补充一些RESTFul API知识:

GET 请求:获取服务器中的对象

相当于SQL的Select命令

GET /blogs:列出所有博客信息

POST 请求:在服务器上新增对象

相当于SQL的Update命令

POST /blogs/ID:新增指定的博客信息

PUT 请求:在服务器上更新对象

相当于SQL的Create命令

PUT /blogs/ID:更新一个博客信息

DELETE 请求:删除服务器中的对象

相当于SQL的Delete命令

DELETE /blogs/ID:删除指定的博客

HEAD 请求:仅仅用于获取对象的基础信息

7.2 POST请求

ps:-H指定请求header,-d指定参数

CURL POST:

[root@localhost ~]# curl -H "Content-Type:application/json" -X POST localhost:9200/company/employees/1 -d '{"id" :1,"name":"admin","password":"123"}'

1

2

7.3 GET请求

CURL GET:

[root@localhost ~]# curl -X GET localhost:9200/company/employees/1?pretty=true

1

7.4 DELETE请求

CURL DELETE:

[root@localhost ~]# curl -X DELETE localhost:9200/company/employees/1

1

7.5 PUT请求

CURL PUT:

curl -H "Content-Type:application/json" -X PUT localhost:9200/company/employees/1 -d '{"id" :1,"name":"admin","password":"111"}'

1

curl默认是GET方式请求url的,要调POST等接口,必须加上-X参数指定

八. CURL参考资料

CURL 官网教程:https://curl.haxx.se/docs/httpscripting.html

阮一峰老师教程:http://www.ruanyifeng.com/blog/2011/09/curl.html

Linux

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:【滚雪球学 Python】2. 无门槛学会数据类型与输入、输出函数,(滚雪球理论)
下一篇:【2022 年】Python3 爬虫教程 - OpenCV 图像匹配识别滑动验证码缺口(2022年高考时间)
相关文章