HTTP簡單認(rèn)識(shí)

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: */*
>
  1. 第一部分為:動(dòng)詞+路徑+協(xié)議/版本(第一行)
  2. 第二部分每行格式:key1: value1
    上面引用代碼中 'Host'; 'User-Agent'; 'Accept'都是key,冒號(hào)后面的為value
  3. 第三部分為:回車("\n",就是空著無內(nèi)容的一行,用以分隔第二和第四部分,見上面代碼第5行)
  4. 第四部分為:要上傳的數(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&amp;tpl=mn&amp;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>&copy;2017&nbsp;Baidu&nbsp;<a href=http://www.baidu.com/duty/>使用百度前必讀</a>&nbsp; <a href=http://jianyi.baidu.com/ class=cp-feedback>意見反饋</a>&nbsp;京ICP證030173號(hào)&nbsp; <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)容

  1. 打開Chrome
  2. 按F12,打開network
  3. 在導(dǎo)航欄輸入網(wǎng)址
  4. Name下點(diǎn)擊網(wǎng)址,在傍邊的Headers可以看到Response Headers以及Request Headers,分別為響應(yīng)和請(qǐng)求。

查看請(qǐng)求部分,如下圖流程


image
  1. 點(diǎn)擊Request Headers,再點(diǎn)擊view source這步很重要,一定要點(diǎn)擊view source,如下圖
    image
  2. 然后會(huì)看到請(qǐng)求第一部分和第二部分的內(nèi)容


    image
  3. 第四部分可以點(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)詞。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,715評(píng)論 19 139
  • HTTP簡介 HTTP(Hypertext Transfer Protocol,超文本傳輸協(xié)議 )是在萬維網(wǎng)上進(jìn)行...
    江湖豎子閱讀 681評(píng)論 0 0
  • http簡介 http的英文名稱是:Hypertext transfer protocol.Tim Berners...
    瘋狂的蝸牛Dianna閱讀 961評(píng)論 0 2
  • 什么是HTTP? 在Web應(yīng)用中,服務(wù)器把網(wǎng)頁傳給瀏覽器,實(shí)際上就是把網(wǎng)頁的HTML代碼發(fā)送給瀏覽器,讓瀏覽器顯示...
    Frank_io閱讀 303評(píng)論 0 0
  • 兩年后,蘇予坐在椅子上手捧著茶,眼睛盯著照片出神,想著,當(dāng)初她沒有做那個(gè)決定,是不是結(jié)局就會(huì)不一樣。哪怕是錯(cuò),她也...
    舒家小妹閱讀 449評(píng)論 0 3

友情鏈接更多精彩內(nèi)容