HTTP message format

Source: Internet
Author: User
Tags error status code

When a user enters an HTTP-based URL (a URL starting with http: //) in the browser, it is equivalent to notifying the browser to organize an HTTP request according to the URL, and send the request to the server. At the same time, wait for the response from the server. Both requests and responses are called HTTP messages.

HTTP is divided into two parts:

1. http request message.

2. HTTP Response Message.


1. HTTP Request Message


HTTP request messages are divided into request message headers and Request Entity content.

1. Request Message Header


(1) The first line of the Request Message Header must consist of the following three parts:

A. Request methods (get, post, Head, etc)

B. Web Resource path (http://www.website.com/test/test.html/test/test.html)

Part)

C. version of HTTP protocol (HTTP/1.0 or HTTP/1.1)


(2) Other lines in the request header are request header fields. The format of each row is:

"Header field" ":" "header field value"


(3) The request header ends with a blank line.

The following is an example of a GET request:

GET / HTTP/1.1Host: www.csdn.netConnection: Keep-AliveAccept: */*

For a POST request, the object content to be submitted is placed behind the blank line in the message header. The following is an example of a POST request:

POST /servlets-examples/servlet/RequestParamExample HTTP/1.1Host: localhost:8888Content-Length: 29Connection: Closefirstname=Bill&lastname=Gates

In the preceding request message, Content-Length indicates the length of the request content in bytes ("firstname = Bill & lastname = Gates ). When using the POST method, this field must be provided and the length must be equal to the length of the object content. Otherwise, the server returns an error status code.

Ii. HTTP Response Message

HTTP response messages are also divided into message headers and entity content. The HTTP Response Message Header is similar to the request message header. The first line is the request result, that is, the first line in the response header indicates whether the request message has successfully obtained web resources on the server. The first line must consist of the following three parts:

1. HTTP Version of the Response Message

The format is HTTP/1.1 or HTTP/1.0. This version number may not be the same as the version number of the Request Message Header. This is mainly because the server may not support the HTTP Version described in the HTTP request, such as using get/HTTP/1.1 to request the server, if the server only supports http1.0, HTTP/1.0 is returned.

2. Status Code

This status code consists of three digits and is divided into five grades. The response code of http1.1 is as follows:

(1) numbers starting with 1 (1xx)

The status code of the temporary request. It consists of 100 and 101. These status codes are not frequently used. They are mainly used as the temporary status returned by the server for the client.

(2) number starting with 2 (2XX)

Request success status code. The value range is from 200 to 206. The most common one is 200, which indicates that the client request is successful and the server has returned the requested web resource to the client. The other six status codes are similar, indicating that the request is successful. They only need to guide the client to the next step. For example, Status Code 206 indicates that the server only returns a part of the requested resources. To obtain all the web resources, the client must send an HTTP request. For more information about the meaning of other response codes, see rfc2616.

(3) numbers starting with 3 (3xx)

Web Resource redirection status code. The value range is from 300 to 307. All status codes starting with 3 change the original URL for Web Resources for different reasons and methods. For example, 302 uses a location field to determine the location after the web resource changes the URL. With this status code, when the URL of a website or other web resources changes, users accessing these web resources cannot even know the changed URL. Therefore, you can add a 302 response when the user accesses the original URL to enable the client to automatically access the new URL.

(4) number starting with 4 (4xx)

Client error status code. The value range is from 400 to 417. Maybe no one wants to see this status code. But they do exist in large numbers on the Internet. If the web resource you access does not exist or you do not have the permission to access the Web resource, the server returns this type of status code. This type of status code is most often encountered in 400. When a user sends a non-existing Web Resource path (the path following the get, post, and head methods), the server returns this status code.

(5) number starting with 5 (5xx)

Server Error status code. The value range is from 500 to 505. This type of status code is also a kind of error status code, but it is different from 4xx, 5xx error is caused by the server; if you send a method that is not supported by the HTTP protocol to the server, for example, get1, the Server Returns Error 501, indicating that the server does not support this HTTP request method.

3. Status Information

The content of the response is closely related to the status code. If the status code is 200, the status information is "OK ". The status code is 501, And the status information is "not implemented ". For more information about the status of each status code, see rfc2616.

The other parts of the HTTP Response Message are the same as the request message. It also consists of many response header fields, each of which occupies one line. The Response Message Header ends with a blank line, followed by the HTTP Response Message entity content. The following is an example of a complete HTTP request and response:

HTTP Request Message:

GET / HTTP/1.1Host:www.csdn.net

HTTP Response Message:

HTTP/1.0 200 OKContent-Length: 132273Content-Type: text/htmlContent-Location: http://www.csdn.net/index.htmLast-Modified: Sun, 28 Jan 2007 09:20:00 GMTAccept-Ranges: bytesETag: "eed72b7cbd42c71:1b0e"Server: Microsoft-IIS/6.0X-Powered-By: ASP.NETDate: Sun, 28 Jan 2007 09:23:26 GMTAge: 32X-Cache: HIT from cachebj244.csdn.netConnection: close<!DOCTYPE html 

From the preceding HTTP message, we can see that the HTTP versions in the request and response messages are not the same. This indicates that the csdn web server adopts the http1.0 protocol to be compatible with clients that only support http1.0.

Note: The request method (get, post, etc.) in the first line of the HTTP request and response message, as well as the letters in HTTP/1.1 or http1.0, must be capitalized, and the path after the request method is case sensitive. The header field is case-insensitive, but most request and response field names are capitalized.

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.