Java network programming from getting started to mastering: Introduction to the HTTP protocol

Source: Internet
Author: User

First, what is the HTTP protocol

The HTTP protocol is an application-layer protocol, and HTTP is an acronym for the Hypertext Transfer Protocol ( Hypertext Transfer Protocol ). HTTP can transfer data between the client and the server through the TCP protocol of the Transport layer. the HTTP protocol is primarily used for data exchange between Web browsers and Web servers . When we use IE or Firefox to browse the Web or download Web resources, by entering Http://host:port/path in the Address bar, the first 4-letter HTTP is equivalent to notifying the browser to use the HTTP protocol to communicate with the server determined by host.

The HTTP protocol was born in the early 90; the first widely used version was HTTP0.9. This initial version is very primitive, it only sends a very simple request to the server, and the server returns a very simple response as well as the corresponding HTML text . In the subsequent HTTP1.0, many features that are not in the HTTP0.9 are added, such as the addition of resource relocation, a large number of status response codes, and so on. In the latest HTTP1.1, the HTTP1.0 has been further improved, in addition to a number of request methods, the biggest improvement is to keep the HTTP connection status. This is useful for applications that frequently transmit data. Since the HTTP protocol has reached its goal, the HTTP1.1, which is responsible for making the specification, has stopped improving HTTP (and HTTP-related protocols or extensions have not stopped), so the final version of the HTTP protocol will be the last.

Whether you are engaged in web-application development, on-line development, or a maintenance person on your website, you must have a more in-depth understanding of the HTTP protocol. Therefore, the HTTP protocol is not only the most widely used protocol on the Internet, but also a relatively simple entry-level protocol in the application protocol family, and all Web servers support the HTTP protocol without exception. This also fully illustrates that for developers who develop network programs, especially those that develop various types of Web servers, a thorough understanding of the HTTP protocol will have a direct impact on the HTTP protocol-based systems you develop.

Since the purpose of this article is not to explain the HTTP protocol, only the main part of the HTTP protocol is discussed, and if readers are interested in the HTTP protocol and want to learn more about the HTTP protocol, check the RFC2616 or www.w3c.org to learn more about the HTTP protocol.

Second, HTTP Way of working

The HTTP protocol uses a request/response approach. After each request to the server, a client based on the HTTP1.0 protocol returns a response message to the client (including whether the request is correct and the requested data), and after confirming that the client has received a response message, the server shuts down the network connection (actually shutting down the TCP connection). In this data transfer process, no historical information and status information is saved, so the HTTP protocol is also considered a stateless protocol, figure 1 depicts the communication process of the HTTP1.0 protocol.

Figure 1 Communication process for the HTTP1.0 protocol

In the HTTP1.0 protocol, when a Web browser makes a request, it means that a request/response session has already started. After the request, the response ends, the server shuts down the connection immediately. This conversational approach is simple, but it brings another problem. If a client browser accesses an HTML or other type of Web page that contains other Web resources, such as JavaScript files, image files, CSS files, and so on, when the browser encounters such a Web resource, an HTTP session is established. If such a large number of resources, it will increase the burden on the server, but also affect the client browser loading HTML and other Web resources efficiency.

After improving and perfecting the above defects, the HTTP1.1 agreement entered our sight. The biggest difference between HTTP1.1 and HTTP1.0 is the increased support for persistent connections. When the client connects to the server using the HTTP1.1 protocol, the server returns the initiative of shutting down the client connection to the client, that is, after the client sends a request to the server and receives a response, the network connection is closed as long as the close method of the socket class is not called. You can continue to send HTTP requests to the server. when the HTML contains other Web resources, the browser can use the same network connection to download these resources, which can greatly reduce the pressure on the server. Figure 2 illustrates this process.

Figure 2 Communication process for the HTTP1.1 protocol

In addition to supporting persistent connections , the HTTP1.1 extends the HTTP1.0 request method from the original three (get, POST, and head) to eight (OPTIONS,, POST, PUT, DELETE, Trace and connect). It also adds a lot of request and response fields, such as the above persistent connection field connection. This field has two values, close and keep-alive. If you use Connection:close to turn off HTTP1.1 's persistent connection, you must use Connection:keep-alive to turn on the HTTP1.1 persistent connection feature. Or do not add the connection field (because HTTP1.1 is persistent by default). In addition to these, the related request headers and response headers, such as identity authentication, state management, and cache, are also provided.

Java network programming from getting started to mastering: Introduction to the HTTP protocol

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.