curl命令
curl是一種命令行工具,支持多種協(xié)議,作用是發(fā)出網(wǎng)絡請求,然后得到和提取數(shù)據(jù),顯示在stdout上面。
在服務器端,使用curl查看請求地址是否返回數(shù)據(jù)來判斷是nginx配置問題還是程序服務配置問題不失為一種好方式(curl -i "...")!
curl www.sina.com <html> <head><title>301 Moved Permanently</title></head> <body bgcolor="white"> <center><h1>301 Moved Permanently</h1></center> <hr><center>nginx</center> </body> </html>如果要把這個網(wǎng)頁保存下來,可以使用-o參數(shù),這就相當于使用wget命令了。
curl -o [文件名] www.sina.com
curl -L www.sina.com鍵入上面的命令,結(jié)果就自動跳轉(zhuǎn)為www.sina.com.cn并顯示源碼。
-i參數(shù)可以顯示http response的頭信息,連同網(wǎng)頁代碼一起。
curl -I www.sina.com curl -i www.sina.com HTTP/1.1 301 Moved Permanently Server: nginx Date: Wed, 17 Jun 2015 13:54:42 GMT Content-Type: text/html Location: http://www.sina.com.cn/ Expires: Wed, 17 Jun 2015 13:56:42 GMT Cache-Control: max-age=120 Age: 102 Content-Length: 178 X-Cache: HIT from xd33-96.sina.com.cn<html> <head><title>301 Moved Permanently</title></head> <body bgcolor="white"> <center><h1>301 Moved Permanently</h1></center> <hr><center>nginx</center> </body> </html>
curl -v www.sina.com查看更詳細的通信過程:
curl --trace output.txt www.sina.com或者
curl --trace-ascii output.txt www.sina.com運行后,請打開output.txt文件查看。
curl -X POST www.example.com curl -X DELETE www.example.com
curl --referer http://www.example.com http://www.example.com
curl --user-agent "[User Agent]" [URL]
curl --cookie "name=xxx" www.example.com
curl --header "Content-Type:application/json" http://example.com
curl --user name:password example.com
參考自: http://www.ruanyifeng.com/blog/2011/09/curl.html
在服務器端,使用curl查看請求地址是否返回數(shù)據(jù)來判斷是nginx配置問題還是程序服務配置問題不失為一種好方式(curl -i "...")!
一、查看網(wǎng)頁源碼
直接在curl命令后加上網(wǎng)址,就可以看到網(wǎng)頁源碼:curl www.sina.com <html> <head><title>301 Moved Permanently</title></head> <body bgcolor="white"> <center><h1>301 Moved Permanently</h1></center> <hr><center>nginx</center> </body> </html>如果要把這個網(wǎng)頁保存下來,可以使用-o參數(shù),這就相當于使用wget命令了。
curl -o [文件名] www.sina.com
二、自動跳轉(zhuǎn)
有的網(wǎng)址是自動跳轉(zhuǎn)的。使用-L參數(shù),curl就會跳轉(zhuǎn)到新的網(wǎng)址。curl -L www.sina.com鍵入上面的命令,結(jié)果就自動跳轉(zhuǎn)為www.sina.com.cn并顯示源碼。
三、顯示頭信息
-I參數(shù)則是只顯示http response的頭信息。-i參數(shù)可以顯示http response的頭信息,連同網(wǎng)頁代碼一起。
curl -I www.sina.com curl -i www.sina.com HTTP/1.1 301 Moved Permanently Server: nginx Date: Wed, 17 Jun 2015 13:54:42 GMT Content-Type: text/html Location: http://www.sina.com.cn/ Expires: Wed, 17 Jun 2015 13:56:42 GMT Cache-Control: max-age=120 Age: 102 Content-Length: 178 X-Cache: HIT from xd33-96.sina.com.cn<html> <head><title>301 Moved Permanently</title></head> <body bgcolor="white"> <center><h1>301 Moved Permanently</h1></center> <hr><center>nginx</center> </body> </html>
四、顯示通信過程
-v參數(shù)可以顯示一次http通信的整個過程,包括端口連接和http request頭信息。curl -v www.sina.com查看更詳細的通信過程:
curl --trace output.txt www.sina.com或者
curl --trace-ascii output.txt www.sina.com運行后,請打開output.txt文件查看。
五、發(fā)送表單信息
發(fā)送表單信息(GET): curl example.com/form.cgi?data=xxx發(fā)送表單信息(POST): curl --data "data=xxx" example.com/form.cgi發(fā)送表單信息,數(shù)據(jù)編碼(POST): curl --data-urlencode "date=April 1" example.com/form.cgi六、HTTP動詞
curl默認的HTTP動詞是GET,使用-X參數(shù)可以支持其他動詞。curl -X POST www.example.com curl -X DELETE www.example.com
七、Referer字段
有時需要在http request頭信息中,提供一個referer字段,表示是從哪里跳轉(zhuǎn)過來的。curl --referer http://www.example.com http://www.example.com
八、User Agent字段
這個字段是用來表示客戶端的設備信息。服務器有時會根據(jù)這個字段,針對不同設備,返回不同格式的網(wǎng)頁。curl --user-agent "[User Agent]" [URL]
九、cookie
curl發(fā)送cookie:curl --cookie "name=xxx" www.example.com
十、增加頭信息
有時需要在http request之中,自行增加一個頭信息。--header參數(shù)可以起到這個作用。curl --header "Content-Type:application/json" http://example.com
十一、HTTP認證
有些網(wǎng)域需要HTTP認證,這時curl需要用到--user參數(shù)。curl --user name:password example.com
參考自: http://www.ruanyifeng.com/blog/2011/09/curl.html
總結(jié)
- 上一篇: 九度OJ 1103:二次方程计算器 (解
- 下一篇: 输出杨辉三角的前N行