In doing Web front-end development, we have to know how the client and the service side is how to communicate, popular simple point is the request (client) > response (server) process, but how the client is to the server how to request it. And the service side is how to respond to it, the response information is not what we want it. Look down with the question.
HTTP (hypertext Transfer Protocol) is a set of rules by which a computer communicates over a network. Computer experts designed HTTP to enable HTTP clients, such as Web browsers, to request information and services from an HTTP server (Web server). The current version of the HTTP protocol is 1.1.HTTP is a stateless protocol, stateless means that there is no need to establish a persistent connection between the Web browser and the Web server, which means that when a client makes a request to the server and then the Web server returns a response (response), The connection is closed and information about the connection is not preserved on the server side. HTTP follows request/answer (Response) model. The Web browser sends a request to the Web server, which processes the request and returns the appropriate answer. All HTTP connections are constructed into a set of requests and replies.
HTTP uses content types, meaning that the files returned by the Web server to the Web browser have types associated with them. All of these types are modeled on the Mimeinternet Mail protocol, where the Web server tells the Web browser what kind of file it is, whether it is an HTML document, an GIF format image, a sound file, or a stand-alone application. Most Web browsers have a series of configurable helper applications that tell the browser what to do with the various content types that the Web server sends over.
The HTTP communication mechanism is that during a full HTTP communication process, the following 7 steps are completed between the Web browser and the Web server:
(1) Establish a TCP connection
Before HTTP work begins, the Web browser first establishes a connection to the Web server over the network, which is done through TCP, which, together with the IP protocol, builds 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 layer protocol than TCP, according to the rules, only after the low-level protocol has been established in order to make a more layer of protocol connectivity, so first to establish a TCP connection, the general TCP connection port number is 80
(2) Web browser sends request command to Web server
Once 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 send Request header information
After the browser sends its request command, it also sends some other information to the Web server in the form of header information, after which the browser sends a blank line to notify the server that it has ended sending the header message.
(4) Web server answer
After the client makes a request to the server, the server sends a reply back to the client.
http/1.1 OK
The first part of the answer is the version number of the protocol and the Response status code
(5) The Web server sends the reply header information
Just as the client sends information about itself along with the request, the server also sends a reply to the user about its own data and the requested document.
(6) The Web server sends data to the browser
After the Web server sends header information to the browser, it sends a blank line to indicate that the header message was sent to this end, and then it sends the actual data requested by the user in the format described in the Content-type reply header information
(7) The Web server shuts down the TCP connection
Typically, once the Web server sends the request data to the browser, it shuts down the TCP connection, and if the browser or server adds the line of code to its header information
Connection:keep-alive
The TCP connection remains open after it is sent, so the browser can continue to send requests through the same connection. Keeping the connection saves the time it takes to establish a new connection for each request, and also saves network bandwidth.
HTTP request Format
When the browser makes a request to the Web server, it passes a block of data to the server, the request information, which consists of 3 parts:
L Request Method URI Protocol/version
L Request Header (requests header)
L Request Body
The following is an example of an HTTP request:
get/sample.jsphttp/1.1
accept:image/gif.image/jpeg,/
Accept-language:zh-cn
Connection:keep-alive
Host:localhost
user-agent:mozila/4.0 (compatible; MSIE5.01; Window NT5.0)
Accept-encoding:gzip,deflate
username=jinqiao&password=1234
(1) Request method URI Protocol/version
The first line of the request is "method URL negotiation/version": Get/sample.jsp http/1.1
In the above code, "get" represents the request method, "/sample.jsp" represents the URI, and "http/1.1 represents the version of the Protocol and Protocol."
Depending on the HTTP standard, HTTP requests can use multiple request methods. For example: HTTP1.1 supports 7 request methods: 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 resources to be accessed, usually by giving a relative directory relative to the server's root directory, so always starts with "/", and finally, the Protocol version declares the version of HTTP used in the communication process.
(2) Requested header (Request header)
The request header contains a number of useful information about the client environment and the body of the request. For example, the request header can declare the language used by the browser, request the length of the body, and so on.
accept:image/gif.image/jpeg./
Accept-language:zh-cn
Connection:keep-alive
Host:localhost
user-agent:mozila/4.0 (Compatible:msie5.01:windows NT5.0)
Accept-encoding:gzip,deflate.
(3) Request body
Between the request header and the request body is a blank line, which is very important, indicating that the request header has ended and the request body is followed. The request body can contain query string information submitted by the customer:
username=jinqiao&password=1234
In the HTTP request for the example above, the body of the request has only one line of content. Of course, in practical applications, the HTTP request body can contain more content.
HTTP request method I only discuss the Get method and post method here
L Get method
The Get method is the default HTTP request method that we use to submit the form data on a daily basis, but the form data submitted with the Got method is simply encoded, and it will be sent as part of the URL to the Web server, so If you use the Get method to submit the form data, there is a security risk. For example
Http://127.0.0.1/login.jsp?Name=zhangshi&Age=30&Submit=%cc%E+%BD%BB
From the URL request above, it is easy to identify the content submitted by the form. (。 In addition, because the data submitted by the Get method is part of the URL request, the amount of data submitted cannot be too large
L POST method
The Post method is an alternative to the Get method, which mainly submits form data to the Web server, especially large quantities of data. The Post method overcomes some of the drawbacks of the Get method. When submitting form data through the Post method, the data is sent to the Web server as the standard data instead of as part of the URL request, which overcomes the disadvantage that the information in the Get method cannot be kept secret and the data amount is too small. Therefore, for security reasons and respect for user privacy, the Post method is usually used for submitting forms.
From a programmatic standpoint, if a user submits data through a GET method, the data is stored in the QUERY_STRING environment variable, and the data submitted by the Post method can be obtained from the standard input stream.
HTTP replies are similar to HTTP requests, and HTTP responses are made up of 3 components, respectively:
L Protocol Status Version Code description
L Response Header (Response header)
L Response Body
The following is an example of an HTTP response:
http/1.1 OK
Server:apache tomcat/5.0.12
date:mon,6oct2003 13:23:42 GMT
content-length:112