初學者都該了解的 HTTP 通訊協定基礎
Hypertext Transfer Protocol
多奇數位創意有限公司
技術總監 黃保翕 ( Will 保哥 )
部落格:http://blog.miniasp.com/
超文本傳輸協定 ( HTTP )
Hypertext Transfer Protocol
目前 HTTP 通訊協定的版本
• HTTP/0.9
– 於 1991 年發表 (已廢止)
• HTTP/1.0
– 於 1996 年 5 月發表( RFC 1945 )
• HTTP/1.1
– 於 1997 年 1 月發表( RFC 2068 )
– 於 1999 年 6 月發布 HTTP/1.1 更新版 ( RFC 2616 )
– 於 2014 年 6 月再次更新 HTTP/1.1 並將規格文件拆成六份
• HTTP/2 (Wikipedia)
– 於 2015 年 5 月發布 ( RFC 7540 ) (瀏覽器相容性)
– 僅針對 HTTP/1.1 的 Message Syntax 部分做出強化
3
HTTP/0.9
• 於 1990 年由 Tim Berners-Lee 提出最初的 HTTP 建議
( WWW )
• 實作重點
– 用戶端要求是以 ASCII 字元為主 ( 單行命令即可發出要求  GET / )
– 用戶端要求會以一個 換行字元 (CRLF) 來結束
– 伺服器回應是以 ASCII 字元為主,回應內容為是 HTML 文件
– 每次執行完 Request / Response 就會自動關閉連線
• 主要特點
– Client / Server 架構、Request / Response 協定
– 跑在 TCP/IP 上的 ASCII 協定 (要求/回應)
– 被設計用來傳輸 HTML 文件
– 每完成一次 ASCII 協定就會自動關閉連線
4
5
$ telnet google.com 80
Trying 172.217.24.14...
Connected to google.com.
Escape character is '^]'.
GET /about
HTTP/1.0 301 Moved Permanently
Location: http://www.google.com/about/
Content-Type: text/html; charset=UTF-8
X-Content-Type-Options: nosniff
Date: Sat, 17 Jun 2017 03:23:44 GMT
Expires: Mon, 17 Jul 2017 03:23:44 GMT
Server: sffe
Content-Length: 225
X-XSS-Protection: 1; mode=block
Cache-Control: public, max-age=2592000
Age: 252414
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>301 Moved</TITLE></HEAD><BODY>
<H1>301 Moved</H1>
The document has moved
<A HREF="http://www.google.com/about/">here</A>.
</BODY></HTML>
Connection closed by foreign host.
HTTP/1.0
• 在 1991 到 1995 年之間,所謂 瀏覽器 騰空出世!
• 於 1996 年 5 月發表 HTTP/1.0 版 ( RFC 1945 )
• 與 HTTP/0.9 不同的部分
– 用戶端要求是以 ASCII 字元為主,但可發送多行命令 (含要求標頭)
• 先送出要求方法,再送出要求標題,最後以一個 額外的換行字元 結束
– 伺服器回應是以 ASCII 字元為主,回應內容會區分
• 狀態列
• 回應標頭
• 回應內文主體 ( 不僅僅是 HTML 文件,可以是更多內容類型 )
– 每完成一次 Request / Response 交握就會自動關閉 TCP/IP 連線
6
7
$ telnet google.com 80
Trying 172.217.24.14...
Connected to google.com.
Escape character is '^]'.
GET /about HTTP/1.0
HTTP/1.0 301 Moved Permanently
Location: http://www.google.com/about/
Content-Type: text/html; charset=UTF-8
X-Content-Type-Options: nosniff
Date: Sat, 17 Jun 2017 03:23:44 GMT
Expires: Mon, 17 Jul 2017 03:23:44 GMT
Server: sffe
Content-Length: 225
X-XSS-Protection: 1; mode=block
Cache-Control: public, max-age=2592000
Age: 252414
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>301 Moved</TITLE></HEAD><BODY>
<H1>301 Moved</H1>
The document has moved
<A HREF="http://www.google.com/about/">here</A>.
</BODY></HTML>
Connection closed by foreign host.
HTTP/1.1
• 於 1997 年 1 月發表 HTTP/1.1 版 ( RFC 1945 )
• 於 1999 年 6 月發布 HTTP/1.1 更新版 ( RFC 2616 )
• 與 HTTP/1.0 不同的部分
– 傳輸協議的效能改進 ( 新增不少 HTTP 標頭定義 )
• 持續連線狀態 ( persistent connection ) ( MDN )
• 切塊編碼傳輸 ( chunked encoding transfer ) ( MDN )
• 位元範圍請求 ( byte range request ) ( MDN )
• 額外快取機制 ( cache control )
• 請求管線作業 ( request pipelining ) ( 需搭配持續連接狀態特性才能用 )
8
9
$ telnet www.miniasp.com 80
Trying 172.217.24.14...
Connected to google.com.
Escape character is '^]'.
GET / HTTP/1.1
Host: www.miniasp.com
Connection: close
Range: bytes=0-20
HTTP/1.1 206 Partial Content
Content-Length: 21
Content-Type: text/html
Content-Range: bytes 0-20/21540
Last-Modified: Tue, 11 Aug 2015 09:45:01 GMT
Accept-Ranges: bytes
ETag: "d1309b641ad4d01:0"
Server: Microsoft-IIS/8.0
X-Powered-By: ASP.NET
Set-Cookie:
ARRAffinity=de668c30a13e01616d34fb3773ed38c1363f5c626ca6001081524d8948435956;Path
=/;Domain=www.miniasp.com
Date: Tue, 20 Jun 2017 06:23:12 GMT
Connection: close
<!DOCTYPE html>
<htmConnection closed by foreign host.
目前 HTTP/1.1 的六大規格
• RFC 7230 HTTP/1.1: Message Syntax and Routing
• RFC 7231 HTTP/1.1: Semantics and Content (最重要)
• RFC 7232 HTTP/1.1: Conditional Requests
• RFC 7233 HTTP/1.1: Range Requests
• RFC 7234 HTTP/1.1: Caching
• RFC 7235 HTTP/1.1: Authentication
10
關於 HTTP 通訊協定的基本常識
• The Hypertext Transfer Protocol (HTTP) is a stateless
application-level protocol for distributed, collaborative,
hypertext information systems.
– 無狀態特性 ( stateless )
– 分散式架構 ( distributed )
– 協作的架構 ( collaborative )
– 超本文 ( hypertext = not just "text" but with "links" )
11
初學者應了解 HTTP 的基本運作方式
• 如何發出 HTTP 要求 (HTTP Request)
– 要求方法 ( Request Methods )
• GET, POST, DELETE, PUT, PATCH, …
– 要求標頭 ( Request Headers )
• Connection, Content-Type, Accept-Language, …
– 斷行符號 ( CRLF )
– 要求內容主體 ( Request Body )
• 如何回應 HTTP 訊息 (HTTP Response)
– 回應狀態碼 ( Response Status Codes )
• HTTP/1.1 200 OK
– 回應標頭 ( Response Headers )
– 斷行符號 ( CRLF )
– 回應內容主體 ( Response Body) 12
相關連結
• 必備工具
– Postman
– Fiddler free web debugging proxy
• 推薦書籍
– 高效能網站開發指南
– 高效能網站建置指南
– Web 效能優化日誌 Volume 2
• 報名課程
– 2017/07/01 (六) 《台北》ASP.NET Web API 2 開發實戰
– 2017/07/16 (日)《高雄》ASP.NET Web API 2 開發實戰
13
聯絡資訊
• The Will Will Web
記載著 Will 在網路世界的學習心得與技術分享
– http://blog.miniasp.com/
• Will 保哥的技術交流中心 (臉書粉絲專頁)
– http://www.facebook.com/will.fans
• Will 保哥的噗浪
– http://www.plurk.com/willh/invite
• Will 保哥的推特
– https://twitter.com/Will_Huang

初學者都該了解的 HTTP 通訊協定基礎

  • 1.
    初學者都該了解的 HTTP 通訊協定基礎 HypertextTransfer Protocol 多奇數位創意有限公司 技術總監 黃保翕 ( Will 保哥 ) 部落格:http://blog.miniasp.com/
  • 2.
    超文本傳輸協定 ( HTTP) Hypertext Transfer Protocol
  • 3.
    目前 HTTP 通訊協定的版本 •HTTP/0.9 – 於 1991 年發表 (已廢止) • HTTP/1.0 – 於 1996 年 5 月發表( RFC 1945 ) • HTTP/1.1 – 於 1997 年 1 月發表( RFC 2068 ) – 於 1999 年 6 月發布 HTTP/1.1 更新版 ( RFC 2616 ) – 於 2014 年 6 月再次更新 HTTP/1.1 並將規格文件拆成六份 • HTTP/2 (Wikipedia) – 於 2015 年 5 月發布 ( RFC 7540 ) (瀏覽器相容性) – 僅針對 HTTP/1.1 的 Message Syntax 部分做出強化 3
  • 4.
    HTTP/0.9 • 於 1990年由 Tim Berners-Lee 提出最初的 HTTP 建議 ( WWW ) • 實作重點 – 用戶端要求是以 ASCII 字元為主 ( 單行命令即可發出要求  GET / ) – 用戶端要求會以一個 換行字元 (CRLF) 來結束 – 伺服器回應是以 ASCII 字元為主,回應內容為是 HTML 文件 – 每次執行完 Request / Response 就會自動關閉連線 • 主要特點 – Client / Server 架構、Request / Response 協定 – 跑在 TCP/IP 上的 ASCII 協定 (要求/回應) – 被設計用來傳輸 HTML 文件 – 每完成一次 ASCII 協定就會自動關閉連線 4
  • 5.
    5 $ telnet google.com80 Trying 172.217.24.14... Connected to google.com. Escape character is '^]'. GET /about HTTP/1.0 301 Moved Permanently Location: http://www.google.com/about/ Content-Type: text/html; charset=UTF-8 X-Content-Type-Options: nosniff Date: Sat, 17 Jun 2017 03:23:44 GMT Expires: Mon, 17 Jul 2017 03:23:44 GMT Server: sffe Content-Length: 225 X-XSS-Protection: 1; mode=block Cache-Control: public, max-age=2592000 Age: 252414 <HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8"> <TITLE>301 Moved</TITLE></HEAD><BODY> <H1>301 Moved</H1> The document has moved <A HREF="http://www.google.com/about/">here</A>. </BODY></HTML> Connection closed by foreign host.
  • 6.
    HTTP/1.0 • 在 1991到 1995 年之間,所謂 瀏覽器 騰空出世! • 於 1996 年 5 月發表 HTTP/1.0 版 ( RFC 1945 ) • 與 HTTP/0.9 不同的部分 – 用戶端要求是以 ASCII 字元為主,但可發送多行命令 (含要求標頭) • 先送出要求方法,再送出要求標題,最後以一個 額外的換行字元 結束 – 伺服器回應是以 ASCII 字元為主,回應內容會區分 • 狀態列 • 回應標頭 • 回應內文主體 ( 不僅僅是 HTML 文件,可以是更多內容類型 ) – 每完成一次 Request / Response 交握就會自動關閉 TCP/IP 連線 6
  • 7.
    7 $ telnet google.com80 Trying 172.217.24.14... Connected to google.com. Escape character is '^]'. GET /about HTTP/1.0 HTTP/1.0 301 Moved Permanently Location: http://www.google.com/about/ Content-Type: text/html; charset=UTF-8 X-Content-Type-Options: nosniff Date: Sat, 17 Jun 2017 03:23:44 GMT Expires: Mon, 17 Jul 2017 03:23:44 GMT Server: sffe Content-Length: 225 X-XSS-Protection: 1; mode=block Cache-Control: public, max-age=2592000 Age: 252414 <HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8"> <TITLE>301 Moved</TITLE></HEAD><BODY> <H1>301 Moved</H1> The document has moved <A HREF="http://www.google.com/about/">here</A>. </BODY></HTML> Connection closed by foreign host.
  • 8.
    HTTP/1.1 • 於 1997年 1 月發表 HTTP/1.1 版 ( RFC 1945 ) • 於 1999 年 6 月發布 HTTP/1.1 更新版 ( RFC 2616 ) • 與 HTTP/1.0 不同的部分 – 傳輸協議的效能改進 ( 新增不少 HTTP 標頭定義 ) • 持續連線狀態 ( persistent connection ) ( MDN ) • 切塊編碼傳輸 ( chunked encoding transfer ) ( MDN ) • 位元範圍請求 ( byte range request ) ( MDN ) • 額外快取機制 ( cache control ) • 請求管線作業 ( request pipelining ) ( 需搭配持續連接狀態特性才能用 ) 8
  • 9.
    9 $ telnet www.miniasp.com80 Trying 172.217.24.14... Connected to google.com. Escape character is '^]'. GET / HTTP/1.1 Host: www.miniasp.com Connection: close Range: bytes=0-20 HTTP/1.1 206 Partial Content Content-Length: 21 Content-Type: text/html Content-Range: bytes 0-20/21540 Last-Modified: Tue, 11 Aug 2015 09:45:01 GMT Accept-Ranges: bytes ETag: "d1309b641ad4d01:0" Server: Microsoft-IIS/8.0 X-Powered-By: ASP.NET Set-Cookie: ARRAffinity=de668c30a13e01616d34fb3773ed38c1363f5c626ca6001081524d8948435956;Path =/;Domain=www.miniasp.com Date: Tue, 20 Jun 2017 06:23:12 GMT Connection: close <!DOCTYPE html> <htmConnection closed by foreign host.
  • 10.
    目前 HTTP/1.1 的六大規格 •RFC 7230 HTTP/1.1: Message Syntax and Routing • RFC 7231 HTTP/1.1: Semantics and Content (最重要) • RFC 7232 HTTP/1.1: Conditional Requests • RFC 7233 HTTP/1.1: Range Requests • RFC 7234 HTTP/1.1: Caching • RFC 7235 HTTP/1.1: Authentication 10
  • 11.
    關於 HTTP 通訊協定的基本常識 •The Hypertext Transfer Protocol (HTTP) is a stateless application-level protocol for distributed, collaborative, hypertext information systems. – 無狀態特性 ( stateless ) – 分散式架構 ( distributed ) – 協作的架構 ( collaborative ) – 超本文 ( hypertext = not just "text" but with "links" ) 11
  • 12.
    初學者應了解 HTTP 的基本運作方式 •如何發出 HTTP 要求 (HTTP Request) – 要求方法 ( Request Methods ) • GET, POST, DELETE, PUT, PATCH, … – 要求標頭 ( Request Headers ) • Connection, Content-Type, Accept-Language, … – 斷行符號 ( CRLF ) – 要求內容主體 ( Request Body ) • 如何回應 HTTP 訊息 (HTTP Response) – 回應狀態碼 ( Response Status Codes ) • HTTP/1.1 200 OK – 回應標頭 ( Response Headers ) – 斷行符號 ( CRLF ) – 回應內容主體 ( Response Body) 12
  • 13.
    相關連結 • 必備工具 – Postman –Fiddler free web debugging proxy • 推薦書籍 – 高效能網站開發指南 – 高效能網站建置指南 – Web 效能優化日誌 Volume 2 • 報名課程 – 2017/07/01 (六) 《台北》ASP.NET Web API 2 開發實戰 – 2017/07/16 (日)《高雄》ASP.NET Web API 2 開發實戰 13
  • 14.
    聯絡資訊 • The WillWill Web 記載著 Will 在網路世界的學習心得與技術分享 – http://blog.miniasp.com/ • Will 保哥的技術交流中心 (臉書粉絲專頁) – http://www.facebook.com/will.fans • Will 保哥的噗浪 – http://www.plurk.com/willh/invite • Will 保哥的推特 – https://twitter.com/Will_Huang