HTTP packets are text-oriented. Each field in the packet is an ASCII string, and the length of each field is uncertain. HTTP has two types of packets: Request Message and Response Message.
1. Request Message
An HTTP Request Message consists of four parts: request line, request header, empty line, and request data.
(1) Request Line
A request line consists of three fields: Request Method Field, URL field, and HTTP Protocol version field, which are separated by spaces. For example, get/index.html HTTP/1.1.
HTTP request methods include get, post, head, put, delete, options, trace, and connect. The most common get and post methods are described here.
Get: when the client wants to read documents from the server, use the get method. The get method requires the server to place the URL-located resource in the data part of the Response Message and send it back to the client. When the get method is used, the request parameters and corresponding values are appended to the URL, and a question mark ("?") is used. It indicates the end of the URL and the start of the request parameter. The length of the transfer parameter is limited. For example,/index. jsp? Id = 100 & OP = bind.
Post: the post method can be used when the client provides more information to the server. The post method encapsulates Request Parameters in the HTTP request data in the form of Names/values, and can transmit a large amount of data.
(2) Request Header
The request header consists of a pair of keywords and values. Each line is a pair. The keywords and values are separated by a colon. The request header notifies the server of client request information. Typical request headers include:
User-Agent: browser type of the request.
Accept: List of content types that can be recognized by the client.
HOST: the requested host name, which allows multiple domain names to share one IP address, that is, the virtual host.
(3) Empty rows
The last request header is followed by an empty line that sends a carriage return and line feed, notifying the server that there are no request headers below.
(4) request data
The request data is not used in the get method, but in the POST method. The post method is applicable when the customer needs to fill out the form. The most frequently used request headers related to request data are Content-Type and Content-Length.
2. Details
An HTTP request consists of three parts: the request line, the message header, and the request body.
The request line starts with a method symbol and is separated by spaces, followed by the requested URI and Protocol version. The format is as follows:
Method Request-Uri http-version CRLF.
The method indicates the request method, the request-Uri is a unified resource identifier, the http-version indicates the HTTP protocol version of the request, and the CRLF indicates the carriage return and line feed (except for the CRLF as the end, separate CR or lf characters are not allowed ).
There are multiple request methods (all methods are capitalized). The methods are described as follows.
Get: request to obtain the resource identified by request-Uri.
Post: append new data to the resource identified by request-Uri.
Head: request to obtain the Response Message Header of the resource identified by request-Uri.
Put: The request server stores a resource and uses request-Uri as its identifier.
Delete: The request server deletes the resource identified by request-Uri.
Trace: the information of the request received by the request server. It is mainly used for testing or diagnosis.
Connect: reserved for future use.
Options: query the server performance or query resource-related options and requirements.
The method name is case sensitive. When the resource for a request does not support the corresponding request method, the server should return the status code 405 (method notallowed ); when the server does not recognize or does not support the corresponding request method, the status code 501 (notimplemented) should be returned ). The HTTP server should at least implement the get and head methods. Other methods are optional. Of course, the implementation supported by all methods should comply with the respective semantic definitions of the following methods. In addition to the preceding methods, the specific HTTP server can also extend the custom methods.
The following is an example of an application.
(1) Get method: when you enter a URL in the address bar of the browser to access the webpage, the browser uses the get method to obtain resources from the server.
For example: Get/form.html HTTP/1.1 (CRLF)
(2) Post method: the request server must accept the data attached to the request, which is often used to submit forms.
For example: Post/Reg. jsp HTTP/(CRLF)
- Accept: image/GIF, image/X-xbit,... (CRLF)
- ..
- HOST: www.nit.edu.cn (CRLF)
- Content-Length: 22 (CRLF)
- Connection: keep-alive (CRLF)
- Cache-control: No-Cache (CRLF)
- (CRLF) // The CRLF indicates the message
The header has ended, before which it is the message header
- User = Jeffrey & Pwd = 1234 // the data submitted below this row
(3) Head Method: It is almost the same as get method. For the response part of the head request, its HTTP header contains the same information as the GET request. With this method, you do not need to transmit the entire resource content to obtain the information of the resource identified by request-Uri. This method is often used to test the validity, accessibility, and recent updates of hyperlinks.
A URL is "http://test.com/ask.asp? The following is an example of a GET request message with name = Liyang:
- Get http://test.com/ask.asp? Name = Liyang HTTP/1.1
- Accept :*/*
- Accept-language: ZH-CN
- Accept-encoding: gzip, deflate
- User-Agent: Mozilla/4.0 (compatible; MSIE 6.0;
- Windows NT 5.1; sv1;. Net CLR 2.0.50727)
- HOST: www.test.com
- Connection: keep-alive
2. Http response packets
HTTP response is composed of three parts: Status line, message header, and response body.
The status line format is as follows:
HTTP-version status-code reason-phrase CRLF
HTTP-version indicates the HTTP protocol version of the server, status-code indicates the response status code sent back by the server, and reason-phrase indicates the text description of the status code. The status code consists of three digits. the first digit defines the response category and has five possible values.
1xx: indicates that the request has been received and continues to be processed.
2XX: Success-indicates that the request has been successfully received, understood, and accepted.
3xx: Redirection-further operations are required to complete the request.
4xx: client error-the request has a syntax error or the request cannot be implemented.
5xx: Server Error -- the server fails to fulfill the valid request.
Descriptions of common status codes and statuses are as follows.
200 OK: the client request is successful.
400 bad request: the client request has a syntax error and cannot be understood by the server.
401 unauthorized: the request is unauthorized. This status code must be used with the WWW-Authenticate header domain.
403 Forbidden: the server receives the request but rejects the service.
404 Not found: the requested resource does not exist. For example, an incorrect URL is entered.
500 internal server error: An Unexpected error occurs on the server.
503 server unavailable: the server cannot process client requests at present and may return to normal after a period of time. For example, HTTP/1.1200 OK (CRLF ).
A request URL is "http://test.com/ask.asp? The following is an example of a response message with name = Liyang:
- HTTP/1.1 200 OK
- Connection: keep-alive
- Date: Thu, 26 Jul 2010 14:00:02 GMT
- Server: Microsoft-Microsoft IIS/6.0
- X-powered-by: ASP. NET
- Content-Length: 280
- Content-Type: text/html
- Set-COOKIE: aspsessionidsaattcsq = joppkdcamh?oicjpgpbjob; Path =/
- Cache-control: Private
- <HTML>
- <Head>
- <Title> yiwangshen </title>
- </Head>
- <Body>
- <B> HTTP Response Message <br> </B>
- <B> test <br> </B>
- </Body>
- </Html>
HTTP request message and Response Message