HTTP comprehension
Introduction
Features
- no connection: process one request in each connection.
- no status: if current transaction needs previous information, it should be transfered again.
- media independent: any type of media that is able to process can be transferred.
URI & URL
URI (Uniform Resource Identifiers), is a abstract concept.
URL (Uniform Resource Locator), a specific URI, can locate a specific resource.
Request
Request contains
- Request line(Method, URL, HTTP version)
- Header(key&value)
- CR LF
- Body(Only in POST method*)
GET
1 | GET /xxxxxxx.jpg HTTP/1.1 # section 1 |
Section1: Request line
Section2: Header
Section3: CR LF
Body not in GET method.
POST
1 | POST / HTTP1.1 # seciton 1 |
Section1: Request line
Section2: Header
Section3: CR LF
Section4: Body(type defined by Content-Type in Header)
GET & POST
Usually GET method pass value through params which is attached to URL, and body is empty. Although in current HTTP version, GET method pass value through body is accepted. We should follow the common sense that do not put value in body.
Respond
Respond contains
- Respond line(HTTP version, Status code, Reason phrase)
- Header
- CR LF
- Body
1 | HTTP/1.1 200 OK # section 1 |
Section1: Respond line
Section2: Header
Section3: CR LF
Section4: Body
Parameters
Status code
code | describe |
---|---|
1xx | infomation |
2xx | success |
3xx | redirection |
4xx | client error |
5xx | server error |
Content-Type
application/x-www-form-urlencoded
default method, only tansfer text
1 | POST HTTP/1.1 |
multipart/form-data
transfer text or files
1 | Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW |
You can transfer text and files at one time. The Body is seperated by boundary.
application/json
assign date type in Body
1 | { |
You need to serialize the data before tansfering.
Reference
https://www.cnblogs.com/ranyonsue/p/5984001.html
https://zhuanlan.zhihu.com/p/45173862
https://www.cnblogs.com/wbl001/p/12050751.html