HTTP request & response
一樣是最近一直需要反覆了解的東西, 今天花大半天了解它們,
p.s. 圖感覺太小可點選便會放大
因為這標題說明的範圍可大可小,
因而此篇只說明自己最近釐清及想了解的地方...
怕思路斷掉, 寫完這篇累死自己... >"<
怕思路斷掉, 寫完這篇累死自己... >"<
p.s. 圖感覺太小可點選便會放大
第一件事是說明在HTTP request & response流程中傳遞的到底是什麼?
回答:
- request中傳遞的是一堆binary, 在此可視為是inputStream(代表"資料的來源",可從中讀取資料)數據流 [以此類推: response中傳遞的還是一堆binary, 在此可視為是outputStream(代表是"資料的目的地",可以寫入資料)] --> inputStream會在serverSocket.accept()後封包成socket物件(由此可知socket物件仍是由binary組成的)。
- 當我們想要讀取/寫入binary時, 可以這麼寫: socket.getInputStream() 和 socket.getOutputStream() --> 但這操作會因太原始而難操作, 於是包裝成DataInputStream和DataOutputStream, 可更方便去操作讀取/寫入binary。
- 寫code的手法: 因為其實request和response中傳遞的header fields可以很多, 所以把DataInputStream解析出的各種header fields包起來存在一個Request物件,以方便寫code時操作Request物件就好, 而不用從DataInputStream中解析後再操作。同理可證, DataOutputStream也可解析出各種header fields包起來存在一個Response物件中, 以方便寫code時操作。
第二件事是說明HTTP、client、server定義 & URI的作用?
回答:
- HTTP定義: HTTP is based on the client-server architecture model and a stateless request/response protocol that operates by exchanging messages across a reliable TCP/IP connection.
- client定義: An HTTP "client" is a program (Web browser or any other client) that establishes a connection to a server for the purpose of sending one or more HTTP request messages.
- server定義: An HTTP "server" is a program ( generally a web server like Apache Web Server or Internet Information Services IIS, etc. ) that accepts connections in order to serve HTTP requests by sending HTTP response messages.
- URI的作用: HTTP makes use of the Uniform Resource Identifier (URI) to identify a given resource and to establish a connection.
因為有URI幫我們建立連結, 讓我們可以傳遞HTTP Messages, 所以第三件事是說明HTTP Messages的內容。
第三件事是說明HTTP Messages?
1.1. HTTP requests and HTTP responses use a generic message format(格式)?
回答:
- This generic message format consists of the following 4 items,如下所述:
1. A Start-line: 可分為request的"A Request-line" & response的"A Status-line"。
舉例一下好了:
- GET /hello.htm HTTP/1.1 (This is Request-Line sent by the client)
- HTTP/1.1 200 OK (This is Status-Line sent by the server)
----(Start-line和header間會有一個空行(CRLF))----
2. Zero or more header (General|Request|Entity) fields followed by CRLF
3. An empty line (i.e., a line with nothing preceding the CRLF)
indicating the end of the header fields
p.s. 因爲2和3之間有一個空行, 而3又是一個空行 --> 由此可知, header和body之間總共需要2個空行
因為有URI幫我們建立連結, 讓我們可以傳遞HTTP Messages, 所以第三件事是說明HTTP Messages的內容。
第三件事是說明HTTP Messages?
1.1. HTTP requests and HTTP responses use a generic message format(格式)?
回答:
- This generic message format consists of the following 4 items,如下所述:
1. A Start-line: 可分為request的"A Request-line" & response的"A Status-line"。
舉例一下好了:
- GET /hello.htm HTTP/1.1 (This is Request-Line sent by the client)
- HTTP/1.1 200 OK (This is Status-Line sent by the server)
----(Start-line和header間會有一個空行(CRLF))----
2. Zero or more header (General|Request|Entity) fields followed by CRLF
3. An empty line (i.e., a line with nothing preceding the CRLF)
indicating the end of the header fields
p.s. 因爲2和3之間有一個空行, 而3又是一個空行 --> 由此可知, header和body之間總共需要2個空行
4. Optionally a message-body
- 可搭配看下圖:
這是request messages & response messages的架構:
這是request messages & response messages的格式:
加碼看一下request messages介紹: (可以知道某些optional request header & 有identifies additional data的request body皆是屬於提供additional data)
加碼看一下response messages介紹:(可以知道response header & 有identifies additional data的response body皆是屬於提供additional data)
1.2. 介紹Header Fields?
- HTTP header fields provide required information about the request or response, or about the object sent in the message body.
- There are 4 types of HTTP message headers:
General-header: These header fields have general applicability for both request and response messages.
Request-header: These header fields have applicability only for request messages.
--> The request-header fields allow the client to pass additional information about the request, and about the client itself, to the server. These fields act as request modifiers.Here is a list of some important Request-header fields that can be used based on the requirement:
a. Accept-Charset
b. Accept-Encoding
c. Accept-Language
d. Authorization
e. Expect
f. From
g. Host
h. If-Match
i. If-Modified-Since
j. If-None-Match
k. If-Range
l. If-Unmodified-Since
m. Max-Forwards
n. Proxy-Authorization
o. Range
p. Referer
q. TE
r. User-Agent
--> You can introduce your custom fields in case you are going to write your own custom Client and Web Server.
Response-header: These header fields have applicability only for response messages.
--> The response-header fields allow the server to pass additional information about the response which cannot be placed in the Status- Line. These header fields give information about the server and about further access to the resource identified by the Request-URI:
a. Accept-Ranges
b. Age
c. ETag
d. Location
e. Proxy-Authenticate
f. Retry-After
g. Server
h. Vary
i. WWW-Authenticate
--> You can introduce your custom fields in case you are going to write your own custom Web Client and Server.
Entity-header: These header fields define meta information about the entity-body or, if no body is present, about the resource identified by the request.
p.s. 上述我想加以說明, 因為會有點易混淆:
a. General-header大多帶的是整個HTTP transaction相關的欄位名稱(field names),所以包含了request和response。下圖從藍色表格可看到這類header會有的欄位名稱:
b. Request-header大多帶的是client本身或request本身的相關的欄位名稱,下圖從藍色表格可看到這類header會有的欄位名稱:
c. Entity-header大多帶的是和"content(內容)"相關的事, 如content-type或content-length, 所以大多會在response中出現,下圖從藍色表格可看到這類header會有的欄位名稱:
1.3. 介紹Message Body?
- The message body part is optional for an HTTP message but if it is available, then it is used to carry the entity-body associated with the request or response. If entity body is associated, then usually Content-Type and Content-Length headers lines specify the nature of the body associated.
- A message body is the one which carries the actual HTTP request data (including form data and uploaded, etc.) and HTTP response data from the server ( including files, images, etc.). Shown below is the simple content of a message body:
<html>
<body>
<h1>Hello, World!</h1>
</body>
</html>
p.s. 下圖這個request(可能是圖片或webfont的request)就未含body部分:
- 一定要看以下兩個連結中所列部分:
連結(看"Examples of Request Message"全部): https://www.tutorialspoint.com/http/http_requests.htm
連結(看"Examples of Response Message"全部): https://www.tutorialspoint.com/http/http_responses.htm
最後一件事是3個補充如下:
連結("HTTP - Message Examples"這篇需全看): https://www.tutorialspoint.com/http/http_message_examples.htm
這張圖是Status Code:
連結(看"Request Method"這區塊便可): https://www.tutorialspoint.com/http/http_requests.htm
文章參考來源:
留言
張貼留言