HTTP is an essential protocol in our network. Next we will give an in-depth explanation of this issue. Then we will analyze the specific operations of the GET method to implement the HTTP protocol. HTTP is used to send and receive messages over the Internet. HTTP is a request-response protocol ?? The client sends a request and the server returns the response to the request. All requests and responses are HTTP packets. The HTTP protocol uses reliable TCP connections, the default port is 80. The first version of HTTP is HTTP/0.9. later it was developed to HTTP/1.0. The latest version is HTTP/1.1. HTTP/1.1 is defined by RFC 2616 。
In HTTP, the session between the Client and Server is always initialized by the Client by establishing a connection and sending an HTTP request packet, the server does not actively contact the client or requires a connection to the client. Both the browser and server can interrupt the connection at any time. For example, when you browse the Web page, you can click the "stop" button at any time to interrupt the current file download process and close the HTTP connection with the Web server 。
1. HTTP request package
HTTP request packets (GET, POST, and other request methods) are composed of three parts: method-URI-Protocol/version, request header, request body. The following is an example of an HTTP request packet (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 packet is method-URI-Protocol/version:
GET is the request method. According to HTTP standards, HTTP protocol requests can use multiple request methods. HTTP 1.1 supports seven request methods: GET, POST, HEAD, OPTIONS, PUT, DELETE, and TRACE. Common Request methods include GET and POST 。
/Index. jsp indicates URI. URI specifies the network resource to be accessed. HTTP/1.1 is the protocol and Protocol version 。
The last line userName = new_andy & password = new_andy is the body, and the body is separated by an empty line (rn) in the HTTP header. here we need to describe it. Content-Length indicates the Length of the body, some body lengths are not described in the header, but are marked with Transfer-Encoding: chunked. For details about the chunked type length calculation method, see RFC 1626 。
The request packet header also contains many useful information about the client environment and request body, which is not described here 。
2. HTTP response packet
Similar to the HTTP request package, it consists of three parts: Protocol-status code-description, response header, and response body. The following is an example of an HTTP response:
- HTTP/1.1 200 OK
- Server: Microsoft-Microsoft IIS/4.0
- Date: Mon, 3 Jan 2005 13:13:33 GMT
- Content-Type: text/html
- Last-Modified: Mon, 11 Jan 2004 13:23:42 GMT
- Content-Length: 90
- <Html>
- <Head>
- <Title> example of interpreting HTTP packets </title>
- Hello WORLD!
- </Body>
- </Html>
The first line of the HTTP response packet is similar to the first line of the HTTP request, indicating that the protocol used is HTTP 1.1, and the server processes the Request status code 200 。
The Response Header also contains a lot of useful information like the request header, for example, server type, date and time, content type, and length. The response body is the HTML page returned by the server. The response header and body are also separated by CRLF 。