wget/curl 测试http的2种命令行工具

wget/curl是两个比较方便的测试http功能的命令行工具,大多数情况下,测试http功能主要是查看请求响应头信息,而给这两个工具加上适当的命令行参数即可轻易做到,其实查man手册就能找到对应的参数选项,不过这里仍然mark一下。

wget的debug选项:
–debug
Turn on debug output, meaning various information important to the developers of
Wget if it does not work properly. Your system administrator may have chosen to
compile Wget without debug support, in which case -d will not work. Please note
that compiling with debug support is always safe—Wget compiled with the debug
support will not print any debug info unless requested with -d.

实例:


可以看到,wget链接请求采用的是HTTP/1.0协议,这是因为我这里使用的wget版本为1.12,自1.13版本后的wget默认也就采用HTTP/1.1协议(http://bzr.savannah.gnu.org/lh/wget/trunk/annotate/head:/NEWS#L46):

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
[root@localhost src]# ./wget --debug 127.0.0.1
DEBUG output created by Wget 1.13.4 on linux-gnu.
 
URI encoding = `UTF-8'
--2012-10-05 21:32:59--  http: //127.0.0.1/
Connecting to 127.0.0.1:80... connected.
Created socket 3.
Releasing 0x09841a20 ( new refcount 0).
Deleting unused 0x09841a20.
 
---request begin---
GET / HTTP/1.1
User-Agent: Wget/1.13.4 (linux-gnu)
Accept: */*
Host: 127.0.0.1
Connection: Keep-Alive
 
---request end---
HTTP request sent, awaiting response...
---response begin---
HTTP/1.1 200 OK
Server: nginx/1.2.0
Date: Fri, 05 Oct 2012 13:32:59 GMT
Content-Type: text/html
Content-Length: 168
Last-Modified: Fri, 05 Oct 2012 09:05:00 GMT
Connection: keep-alive
Accept-Ranges: bytes
 
---response end---
200 OK
Registered socket 3 for persistent reuse.
Length: 168 1
Saving to: `index.html.1'
 
100%[============================================================>] 168         --.-K/s   in 0s     
 
2012-10-05 21:33:04 (3.60 MB/s) - `index.html.1' saved [168/168]

如果wget不带–debug选项,则可以使用-S、–save-headers选项,不过此时只能查看响应头部信息:
-S
–server-response
Print the headers sent by HTTP servers and responses sent by FTP servers.

–save-headers
Save the headers sent by the HTTP server to the file, preceding the actual contents,
with an empty line as the separator.

实例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[root@localhost ~]# wget -S 127.0.0.1
--2012-05-26 12:38:32--  http: //127.0.0.1/
Connecting to 127.0.0.1:80... connected.
HTTP request sent, awaiting response...
   HTTP/1.1 200 OK
   Server: nginx/1.2.0
   Date: Sat, 26 May 2012 04:38:32 GMT
   Content-Type: text/html
   Content-Length: 186
   Last-Modified: Fri, 25 May 2012 02:41:59 GMT
   Connection: keep-alive
   Accept-Ranges: bytes
Length: 186 1
Saving to: “index.html.44”
 
100%[================================================================>] 186         --.-K/s   in 0s     
 
2012-05-26 12:38:32 (4.52 MB/s) - “index.html.44” saved [186/186]
 
[root@localhost ~]#

利用curl的-v查看请求响应头部信息:
-v/–verbose
Makes the fetching more verbose/talkative. Mostly useful for debugging. A line
starting with ’>’ means “header data” sent by curl, ’<’ means "header data"
received by curl that is hidden in normal cases, and a line starting with ’*’
means additional info provided by curl.

Note that if you only want HTTP headers in the output, -i/--include might be the
option you’re looking for.

If you think this option still doesn’t give you enough details, consider using
--trace or --trace-ascii instead.

This option overrides previous uses of --trace-ascii or --trace.

Use -s/--silent to make curl quiet.

实例(可以看到,curl链接请求默认采用的是HTTP/1.1协议):

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
[root@localhost aa]# curl -v 127.0.0.1
* About to connect() to 127.0.0.1 port 80 (#0)
*   Trying 127.0.0.1… connected
* Connected to 127.0.0.1 (127.0.0.1) port 80 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.19.7 (i686-pc-linux-gnu) libcurl/7.19.7 NSS/3.12.7.0 zlib/1.2.3 libidn/1.18 libssh2/1.2.2
> Host: 127.0.0.1
> Accept: */*
>
< HTTP/1.1 200 OK
< Server: nginx/1.2.0
< Date: Sat, 26 May 2012 04:45:12 GMT
< Content-Type: text/html
< Content-Length: 186
< Last-Modified: Fri, 25 May 2012 02:41:59 GMT
< Connection: keep-alive
< Accept-Ranges: bytes
<
<html>
<head>
<title>Welcome to nginx!</title>
</head>
<body bgcolor= "white" text= "black" >
<center><h1>Welcome to nginx!</h1></center>
<center><h1>root:web</h1></center>
</body>
</html>
* Connection #0 to host 127.0.0.1 left intact
* Closing connection #0
[root@localhost aa]#

利用curl的-I选项仅查看响应头部信息:
-I/--head
(HTTP/FTP/FILE) Fetch the HTTP-header only! HTTP-servers feature the command HEAD
which this uses to get nothing but the header of a document. When used on a FTP
or FILE file, curl displays the file size and last modification time only.

实例:

1
2
3
4
5
6
7
8
9
10
11
[root@localhost aa]# curl -I 127.0.0.1
HTTP/1.1 200 OK
Server: nginx/1.2.0
Date: Sat, 26 May 2012 04:43:12 GMT
Content-Type: text/html
Content-Length: 186
Last-Modified: Fri, 25 May 2012 02:41:59 GMT
Connection: keep-alive
Accept-Ranges: bytes
 
[root@localhost aa]#

附:
curl官网:http://curl.haxx.se/
curl维基:http://en.wikipedia.org/wiki/CURL
wget官网:http://www.gnu.org/software/wget/
wget维基:http://en.wikipedia.org/wiki/Wget
curl与wget的对比:http://daniel.haxx.se/docs/curl-vs-wget.html



转载:Lenky个人站点

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值