60 # The basic concept of http

What is HTTP?

Common networks TCP/IPoperate on the basis of protocol suites, and HTTP is a subset. http is based on the tcp protocol, and some specifications are added on the basis of tcp, which are headers. Learning http is to learn what each header does.

TCP/IP protocol family

In simple terms, a protocol is a communication rule, for example: who initiates a request during communication, how to end it, and how to communicate. Collectively referred to as Internet-related protocols TCP/IP.

protocol layering

OSI (Open System Interconnection Open System Interconnection Reference Model) defines a seven-layer model for network interconnection:

  • Physical layer: manages the interconnection between communication devices and network media.
  • Data Link Layer: Provides media access and link management.
  • Network layer: IP address selection and routing.
  • Transport layer: data communication.
  • Session layer: Create, manage and maintain sessions.
  • Presentation layer: data encoding, format conversion, and data encryption.
  • Application layer: Provides various request services for applications or user requests.

As shown below:

When implemented, it is generally layered into four layers:

  • Link layer (things, numbers): The hardware part of the network connection
  • Network layer: IP selects the transmission route (via ip address and mac address) (uses ARP protocol to communicate with mac address)
  • Transport layer: TCP protocol (reliable), UDP protocol, data transmission (HTTP -> TCP, DNS -> UDP)
  • Application layer (meeting, table, response): HTTP, FTP, DNS (an application service that communicates with other computers, communication activities when providing application services to users)

Access page process TCP/IPCommunication protocol:

  • Resolve the domain name through the DNS protocol to obtain the corresponding ip address
  • Transfer data through the HTTP protocol and generate an HTTP request message for the target Web server
  • In order to facilitate communication, the HTTP request message is divided into message segments through the TCP protocol, divided into multiple message segments according to the serial number, and each message segment is reliably transmitted to the other party
  • Search the address of the other party through the IP protocol, and transmit while transferring
  • The message segment received from the other party through the TCP protocol, after reassembling the arriving message segment, reassemble the request message in the original order according to the sequence number
  • Content processing of web server requests via the HTTP protocol

HTTP features

Creating a server must have a specific ip address, a port number, the request object sent by the client to the server, and the response object sent by the server to the client.

  1. Stateless by default: http is a protocol that does not save state, and cookies can be used to manage state
  2. http(1.1) uses keep-alive: the connection will not be disconnected and the link will be kept, preventing unnecessary establishment and disconnection of tcp links caused by each request
  3. Default pipeline: In the past, after sending a request, you need to wait and receive a response before you can send the next one. Now it is all pipelined, and concurrent requests can be made

HTTP disadvantages

  • communication in clear text
  • Do not verify the identity of the communicating party
  • Integrity of content cannot be verified (content may have been tampered with) (can be handled with https)

HTTP method

Request method:

  • GET: pass data through url
  • POST: request body transfer data

tool

  • postman: (visual request tool)
  • curl: requires git to be installed (command line)

RESTFUL style: It is not a specification, but it is to realize the operation of server resources according to different request methods

  • get: request data
  • post: increase data
  • delete: delete data
  • put: modify data
  • options: preflight request (by default, it is possible to send an options request across domains)

Simple request: get and post are simple requests

Complex requests: If custom headers are added to get and post, they will become complex requests. All other methods are complex requests. If a complex request is sent, the options request will be sent first by default.

Cross-domain: If the protocol, domain name, and port number are not equal, it is cross-domain.

Common ways to solve cross-domain:

  • cors (the server resolves cross-domain, adding cross-domain headers)
  • jsonp
  • nginx (reverse proxy)
  • websocket

Uncommon ways to solve cross-domain:

  • iframe
  • postMessage
  • window.name
  • document.domain
  • location.hash

HTTP status code

  • 101:websocket
  • 2xx
    • 200: success
    • 204: Success but no response body
    • 206: Breakpoint resume (return part of the data)curl -v --header Range:bytes=0-1024 www.baidu.com
  • 3xx
    • 301: Permanent Redirect
    • 302: Temporary redirect
    • 304: Server Cache
    • 307: Redirect (will not convert POST to GET)
  • 4xx
    • 400: The client parameter is incorrect
    • 401: No permission, no login
    • 403: No permission, login without permission
    • 404: not found
    • 405: The server does not support this method not allowed method
  • 5xx
    • 500: server problem
    • 502: There is a problem with load balancing
    • 503: The server reports an error

message

requested message

  • Request line: Contains request method, request path (excluding hash), http version number (can pass parameters)
  • Request header: you can also pass data, custom header
  • Request body: you can put data, transfer data binary...

response message

  • Response line: version of http, status code, response phrase
  • Response header: custom response information
  • Response body: the content returned to the browser

Guess you like

Origin blog.csdn.net/kaimo313/article/details/131971064