1、HTTP的請(qǐng)求和響應(yīng)
HTTP的請(qǐng)求和響應(yīng)都包含4個(gè)部分,在命令行輸入
curl -v www.baidu.com
發(fā)出以下請(qǐng)求
> GET / HTTP/1.1
> Host: www.baidu.com
> User-Agent: curl/7.60.0
> Accept: */*
>
- 第一部分為:動(dòng)詞+路徑+協(xié)議/版本(第一行)
- 第二部分每行格式:key1: value1
上面引用代碼中 'Host'; 'User-Agent'; 'Accept'都是key,冒號(hào)后面的為value - 第三部分為:回車("\n",就是空著無內(nèi)容的一行,用以分隔第二和第四部分,見上面代碼第5行)
- 第四部分為:要上傳的數(shù)據(jù)(也可以沒有,上面代碼中也未顯示)
響應(yīng)如下
< 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, 16 Jul 2018 00:48:49 GMT
< Etag: "58860504-94d"
< Last-Modified: Mon, 23 Jan 2017 13:28:36 GMT
< Pragma: no-cache
< Server: bfe/1.0.8.18
< Set-Cookie: BDORZ=27315; max-age=86400; domain=.baidu.com; path=/
<
{ [1040 bytes data]
100 2381 100 2381 0 0 38403 0 --:--:-- --:--:-- --:--:-- 38403<!DOCTYPE html>
<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=百度一下 class="bg s_btn"></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>新聞</a> <a href=http://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>地圖</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>視頻</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>貼吧</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>登錄</a> </noscript> <script>document.write('<a + encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">登錄</a>');</script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">更多產(chǎn)品</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>關(guān)于百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>©2017 Baidu <a href=http://www.baidu.com/duty/>使用百度前必讀</a> <a href=http://jianyi.baidu.com/ class=cp-feedback>意見反饋</a> 京ICP證030173號(hào) <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>
響應(yīng)與請(qǐng)求類似,但是第一部分格式不同,
響應(yīng)第一部分格式為:協(xié)議/版本號(hào) 狀態(tài)碼 狀態(tài)解釋
第四部分為需要下載的內(nèi)容(見最后一行)
2、如何用Chrome開發(fā)者工具查看 HTTP 請(qǐng)求內(nèi)容
- 打開Chrome
- 按F12,打開network
- 在導(dǎo)航欄輸入網(wǎng)址
- 在Name下點(diǎn)擊網(wǎng)址,在傍邊的Headers可以看到Response Headers以及Request Headers,分別為響應(yīng)和請(qǐng)求。
查看請(qǐng)求部分,如下圖流程

image
- 點(diǎn)擊Request Headers,再點(diǎn)擊view source這步很重要,一定要點(diǎn)擊view source,如下圖
image -
然后會(huì)看到請(qǐng)求第一部分和第二部分的內(nèi)容
image - 第四部分可以點(diǎn)擊preview或者response看到。
查看響應(yīng)部分與請(qǐng)求類似,點(diǎn)擊Response Headers。
3、如何使用 curl 命令
curl是利用URL語法在命令行方式下工作的開源文件傳輸工具。
語法為:
curl [option] [url]
輸入以下指令向百度網(wǎng)站發(fā)出請(qǐng)求,百度網(wǎng)站同時(shí)返回響應(yīng)。
curl -v -s www.baidu.com
curl默認(rèn)動(dòng)詞為GET,-s參數(shù)不會(huì)報(bào)錯(cuò)和顯示進(jìn)度; -v參數(shù)可以顯示一次 http 通信的整個(gè)過程
輸入 -x 可以支持其他動(dòng)詞。

