nc是一個強大的網(wǎng)絡工具迷守,可以用它來掃描端口侦鹏,傳輸文件,遠程克隆分區(qū)等等痰憎。詳細信息可以參考這篇博文
除了上面的使用場景之外骡澈,還能用nc來模擬發(fā)送HTTP 請求
test@test-virtual-machine$ nc www.baidu.com 80
GET / HTTP/1.1
<---這里要 換行锅纺,根據(jù)HTTP協(xié)議規(guī)定,HTTP header和報文主體之間通過空行來分隔
HTTP/1.1 302 Moved Temporarily
Date: Thu, 10 May 2018 03:47:06 GMT
Content-Type: text/html
Content-Length: 225
Connection: Keep-Alive
Location: http://www.baidu.com/search/error.html
Server: BWS/1.1
X-UA-Compatible: IE=Edge,chrome=1
BDPAGETYPE: 3
Set-Cookie: BDSVRTM=0; path=/
<html>
<head><title>302 Found</title></head>
<body bgcolor="white">
<center><h1>302 Found</h1></center>
<hr><center>a0d46eadd08a4c61268f5c8b1823b29e70957b7c
Time : Fri Apr 20 10:34:36 CST 2018</center>
</body>
</html>
這就模擬了一次HTTP的GET請求肋殴,但是在nc的交互中輸入比較麻煩囤锉,但nc作為偉大Linux/Unix環(huán)境下的工具是支持重定向的,"<"符號解決您的煩惱护锤。
事先把HTTP header保存到一個文件里官地,header.txt
HEAD / HTTP/1.1
Host: www.baidu.com
注意文件末尾要有空行
然后執(zhí)行
test@test-virtual-machine$ nc www.baidu.com 80 < header.txt
HTTP/1.1 200 OK
Server: bfe/1.0.8.18
Date: Thu, 10 May 2018 03:49:25 GMT
Content-Type: text/html
Content-Length: 277
Last-Modified: Mon, 13 Jun 2016 02:50:40 GMT
Connection: Keep-Alive
ETag: "575e1f80-115"
Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
Pragma: no-cache
Accept-Ranges: bytes
這樣一個HEAD請求就被發(fā)送了出去,你可以修改header.txt的內容烙懦,把method改成GET驱入, POST,也可以加上報文主體氯析,只要符合HTTP請求規(guī)范就行亏较。