HTTP protocol and HTTP packets

Source: Internet
Author: User
Tags rfc

The HTTP protocol is used to send and receive messages over the Internet. The HTTP protocol is a request-response protocol-The client sends a request, the server returns a reply to the request, and all the requests and responses are HTTP packets. The HTTP protocol uses a reliable TCP connection, and the default port is 80. The first version of HTTP was http/0.9, which later developed to http/1.0, and now the latest version is http/1.1. http/1.1 is defined by RFC 2616.

In HTTP, a session between Client/server is always initialized by the client by establishing a connection and sending an HTTP request packet, and the server does not actively contact the client or require a connection with the client. The browser and server can interrupt the connection at any time, for example, you can click the "Stop" button at any time when browsing the Web page to interrupt the current file download process and close the HTTP connection to the Web server.

1 HTTP Request Package
The HTTP request packet (GET, post and other request methods) consists of three parts, namely: Method-uri-Protocol/version, request header, request body. The following is an example of an HTTP request package (GET):

get/index.jsp http/1.1
Accept-language:zh-cn
Connection:keep-alive
host:192.168.0.106
Content-length:37

Username=new_andy&password=new_andy


The first line of the request package is the method-uri-Protocol/version:
Get is the request method, and HTTP requests can use multiple request methods depending on the HTTP standard. HTTP 1.1 supports seven request methods: Get, POST, HEAD, OPTIONS, PUT, delete, and trace, etc., commonly used for request methods are get and POST.

/index.jsp represents a URI. The URI specifies the network resource to be accessed.
http/1.1 is the version of the Protocol and Protocol.
The last line Username=new_andy&password=new_andy is the body, and the body is separated from the HTTP header with a blank line (\ r \ n). Here is a point to note, where Content-length describes the length of the body, and some body length is not indicated in the head, just marked transfer-encoding:chunked. For the length calculation method for the chunked type, see RFC 1626.
The header of the request package also contains many useful information about the client environment and the request body, which is no longer described here.

2 HTTP Reply Package
Similar to the HTTP request package, consists of three parts, namely: protocol-Status code-description, answer header, reply body. The following is an example of an HTTP reply:

http/1.1 OK
server:microsoft-iis/4.0
Date:mon, 3 Jan 2005 13:13:33 GMT
Content-type:text/html
Last-modified:mon, Jan 2004 13:23:42 GMT
Content-length:90

<title> Interpreting HTTP Packet examples </title>Hello world!
</body>


The first line of the HTTP reply packet resembles the first line of the HTTP request, indicating that the protocol used is HTTP 1.1 and that the server is processing the request's status code 200.
The answer header also contains many useful information, such as server type, datetime, content type, and length, as well as the request header. The body of the reply is the HTML page returned by the server. The answer header and the body are also separated by CRLF.

Explanations in the Wiki

Hypertext Transfer Protocol (HTTP,hypertext Transfer Protocol) is one of the most widely used network transmission protocols on the Internet . All WWW documents must comply with this standard. HTTP was originally designed to provide a way to publish and receive HTML pages.

Overview

The development of HTTP was the result of collaboration between the World Wide Web Association and the Internet Working Group, which identified the final version in a series of RFC releases, the most notable being the RFC 2616. In RFC 2616 , this version of http/1.1, which is commonly used today, is defined.

HTTP is a protocol that is used to request and answer between the client and the server. An HTTP client, such as a Web browser, initializes a request by establishing a connection to a special port on the remote host (the default port is 80). An HTTP server waits for a client to send a sequence of requests by listening on a special port, like "get/http/1.1" (the default page used to request a Web server), and optionally receiving a MIME message like an email . This message contains a large number of information header sequences that describe the various aspects of the request, in response to a selected reserved data body. After receiving a sequence of requests (and, if so, a message), the server sends back a reply message, such as "OK", and sends back a message of its own, which may be the requested file, error message, or some other message.

HTTP differs from other TCP -based protocols, such as FTP. In HTTP, once a special request (or a related sequence of requests) is completed, the connection is usually interrupted. This design makes HTTP perfect for the World Wide Web, where the current page is regularly connected to another server page. When the lack of persistent connections becomes a necessary option to maintain the user state, there are occasional problems for web designers. Most of these methods include the use of "cookies".

Here is a secure version of HTTP called Https,https that supports any encryption algorithm, as long as the encryption algorithm can be understood by both sides of the page.

HTTP (and HTTPS) is located by a unique resource locator or by short URLs. Create the syntax for this address positioning in order to link the HTML.

Example

Here is an example of a session between an HTTP client and a server running on www.google.com, port 80

Client Request:

get/http/1.1
Host:www.google.com

(followed by a newline, implemented by typing in enter)

Server Answer:

http/1.1 OK
content-length:3059
server:gws/2.0
Date:sat, Jan 2003 02:44:04 GMT
Content-type:text/html
Cache-control:private
Set-cookie:pref=id=73d4aef52e57bae9:tm=1042253044:lm=1042253044:s=smcc_hrpcqiqy
x9j; Expires=sun, 17-jan-2038 19:14:07 GMT; path=/; Domain=.google.com
Connection:keep-alive

(followed by a blank line, and the HTML-formatted text makes up Google's homepage)

In HTTP1.0, the client sends a request to the server, and the server sends a reply to the client. After that, the connection is freed. On the other hand, HTTP1.1 supports persistent connections. This allows the client to send a request and receive an answer, and then quickly send another request and receive another reply. Because of multiple additional requests, the TCP connection is not freed, and the load on TCP in each request is relatively small. At the same time, it is possible to send multiple requests (usually two) before the answer to the last request is received. This technique is called "pipelining".

HTTP protocol and HTTP packets

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.