HTTP request Explanation

Source: Internet
Author: User

Using the HTTP protocol for front-end communication

The web uses a kind of http (Hypertext Transfer Protocol), Hypertext Transfer Protocol as a specification, complete a series of operational processes from the client to the server. The web is built to communicate on the HTTP protocol.

1.HTTP Communication mechanism during a complete HTTP communication, the following 7 steps will be completed between the Web browser and the Web server:
(1) Establishing a TCP connectionBefore HTTP work begins, the Web browser first establishes a connection to the Web server over the network, which is done through TCP, which works with the IP protocol to build the Internet, known as the TCP/IP protocol family,
So the internet is also known as a TCP/IP network.
HTTP is a higher level of application-level protocol than TCP, according to the rules, only the lower layer protocol is established before it can be a more protocol connection, so the first to establish a TCP connection, the port number of the general TCP connection is 80
(2) Web browser sends request command to Web serverOnce a TCP connection is established, the Web browser sends a request command to the Web server for example: get/sample/hello.jsp http/1.1
(3) Web browser sends request header informationAfter the browser sends its request command, it also sends some other information to the Web server in the form of header information, and then the browser sends a blank line to notify the server that it has ended sending the header information.
(4) Web server answerAfter the client makes a request to the server, the server sends the client a loopback response, http/1.1The first part of the answer is the version number of the protocol and the answer status code
(5) The Web server sends the answer header informationJust as the client sends information about itself along with the request, the server also sends the user with the answer about its own data and the requested document.
(6) The Web server sends data to the browserAfter the Web server sends a header message to the browser, it sends a blank line to indicate that the header information is sent to the end, and then it sends the actual data requested by the user in the format described by the Content-type reply header information
(7) The Web server shuts down the TCP connectionIn general, once the Web server sends the request data to the browser, it closes the TCP connection and then if the browser or server joins this line of code in its header information

HTTP is a stateless protocol. The connection:keep-alivetcp connection will remain open after sending, so the browser can continue to send requests through the same connection. Maintaining a connection saves the time it takes to establish a new connection for each request and also saves network bandwidth.

2.HTTP Request Format2.1 When the browser makes a request to the Web server, it passes a block of data to the server, that is, the request information, the HTTP request information is composed of 3 parts: L Request method URI Protocol/version L request header (request header) L requests Body (1) Request method URI Protocol/version
the first line of the request is "method URL negotiation/version": Http://localhost:63342/webpack/index.html http/1.1 depending on the HTTP standard, HTTP requests can use multiple request methods. For example: HTTP1.1 supports 7 methods of request:GET, POST, HEAD, OPTIONS, PUT, delete, and Tarce. in Internet applications, the most common method is get and post. The URL completely specifies the network resource to be accessed, usually with a relative directory relative to the root of the server, always beginning with "/", and finally, the Protocol version declaresthe version of HTTP used during the communication, "http/1.1 represents the version of the Protocol and Protocol.
(2) The request header request header contains many useful information about the client environment and the request body.  For example, the request header can declare the language used by the browser, the length of the request body, and so on. (3) There is a blank line between the request body request header and the request body, which is very important, it indicates that the request header has ended, and the next is the request body. The request body can contain query string information submitted by the customer:

Insert a knowledge point and briefly describe the difference between a GET request and a POST request:

1. When using a GET request, the parameter is displayed in the URL, and the Post method is used. It won't show up. 2. Use GET request length is limited, send data volume is small, POST request length is unlimited, send data volume is large; 3. From a performance standpoint, the same data can be sent up to twice times the speed of the post. 4. Using a GET request can be cached, and the network data received by the post cannot be cached.

2.2 HTTP reply is similar to HTTP request, HTTP response is also composed of 3 parts, namely: L Protocol Status Version Code description L response Header (Response header) L response Body (1) Protocol status version Code description Protocol status Code It indicates that the protocol used to communicate is that the HTTP1.1 server has successfully processed the request made by the client (200 indicates success):

(2) Response Head (Response header)

The response header (Response header) also contains many useful information, such as server type, datetime, content type, and length, as well as the request header:

(3) Response body

The response body is the HTML page returned by the server:

Insert a knowledge point to list some of the commonly used status code meanings:

1xx-Information Class (information), which indicates receipt of a Web browser request, is being further processed
The 2xx-Success Class (successful), which indicates that the user request is received correctly, is understood and processed for example:
200 the request was successful, and the desired response header or data body will be returned with this response.

The 3xx-redirect Class (redirection) indicates that the request was unsuccessful and the customer must take further action.
303 The response to the current request can be found on another URI, and the client should access that resource in a GET way. This method exists primarily to allow the output of the POST request that is activated by the script to redirect to a new resource.
304 The server should return this status code if the client sends a conditional GET request and the request has been allowed, and the contents of the document (since the last access or condition of the request) have not changed .
  When the client receives a 304 response, the corresponding resource is read from the cache.

4xx-Client error, which indicates that the client submitted a request with an error such as: 404 Not Found, means that the document referenced in the request does not exist.
400 1, the semantic error , the current request can not be understood by the server.   Unless modified, the client should not submit the request repeatedly. 2, the request parameter is wrong.
The 403 server has understood the request, but refuses to execute it.
404 the request failed and the requested resource was not found on the server.
5xx-Server error indicates that the server could not complete the processing of the request:
500 server internal error, in general, this problem will occur when the server's code error occurs.

3. Uri and URL

The URI identifies an Internet resource with a string, and the URL represents the location of the resource. The visible URL is a subset of the URI.

The URI uses a URI that covers all the necessary information, an absolute URL, and a relative URL. A relative URL is a URL that is processed from a basic URI in a browser to see the format of the URI first

HTTP request Explanation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.