3-HTTP protocol

HTTP protocol

  The HTTP protocol, the Hypertext Transfer Protocol, is used to specify the information transfer specifications between the server and the client. Mainly composed of HTTP request message and response message

  • One response corresponds to one request
  • The response is passive and cannot be initiated actively
  • Is built on top of the TCP protocol
  • HTPP default port 80, HTTPS default port 443

Features of HTTP protocol:

  • No connection: the server only processes one request at a time, and immediately disconnects after receiving the response from the client
  • Stateless: the server will not save any traces of the request
  • Simple and fast: the format of the transmitted message is simple and the content is small
  • Flexible: Many types of data can be transmitted, such as html, json, xml, text

HTTP request message

It consists of the following four parts:

1. Request line

  • The first line of the request, and on its own line
  • Including request method, request path, request protocol and version

2. Request header

  • The second line starts to N lines, composed of key-value pairs
  • User-Agent: Tell the server the relevant information of the client
  • Cookie: cookie information brought to the server
  • Referer: Tell the server which page this request was sent from
  • Content-Type:
    ① Indicates the format of data transmission from the client to the server
    ② Common Content-Type values ​​are: x-www-form-urlencoded, JSON, XML, etc. The
    browser defaults to x-www-form-urlencoded format

3, blank line

4. Request message body

  • That is, the request body, the Get request generally does not have the request body

HTTP response message

It consists of the following four parts:

1. Status line

  • Protocol and version, status code and status description composition
  • Status code:
    1xx: message type, generally tells the client that the request has been received and is being processed
    2xx: successfully processed
    3xx: redirection
    4xx: client error, such as requesting a resource that does not exist
    5xx: server error

2. Response header

  • The second line starts to N lines, composed of key-value pairs
  • Content-Type: The format of the data transmitted from the server to the client

3, blank line

4. Response message body

  • Response content (file, json)

HTPP request method

The main request methods are GET, POST, DELETE, PUT, the most commonly used are the first two.

Features of GET method:

  • Unsafe, the data is placed in the request line, and the browser address bar is visible
  • URL length will be limited by the browser, generally within 2000 characters
  • Fast response to requests
  • It is the default request method, it is used when there is no need to submit a large amount of data, no sensitive, no confidential data
  • May be cached
  • The request may be kept in the browser's history

POST:

  • Security, the data is placed in the request body, and the browser address bar is not visible
  • The size of the submitted data is not limited
  • Slow request response
  • Need to submit a large amount of data, use when there is sensitive and confidential data
  • Will not be cached
  • The request will not be kept in the browser history

Cookie和Session

  Because the HTTP protocol is a connectionless state, the server will not keep the state of the request, but when we need the server to remember the state, this requires Cookie, Session or Token

Cookie
Cookie is generated by the server and sent to the client. The browser will store the key/value of the Cookie locally, and send the Cookie to the server the next time the same website is requested.

Use: Use cookies to remember user names

Session
Session is generated by the server and stored in the server memory

  • Session is an object, by the SessionID attribute and invalidate () and other methods
  • Has its own management emergency support, including Session creation, destruction, and timeout mechanisms
  • The way to destroy the Session
    ① The client invokes the invalidate() method of Session's logout
    ② The server is down
    ③ The session is timed out and the server is automatically destroyed.
    Note: Closing the browser will not destroy the session

Cookie and Session to achieve login principle

  1. The server generates a Session and returns the SessionID to the client through the response header
  2. The browser stores this SessonID in the Cookie, and every subsequent request will send all the content in the Cookie to the server through the request
  3. The server obtains the value of the SessionID from the request, and then compares the SessionID with the SessionID in the memory. If the same is the same, the access is allowed, otherwise the access is denied

Guess you like

Origin blog.csdn.net/weixin_45128456/article/details/112623897