HTTP request/Response package format

Source: Internet
Author: User

First, what is the HTTP protocol?

Reprint: http://blog.csdn.net/daijin888888/article/details/51025634

A Network application layer protocol developed by the Web user, which defines the process of communication between the browser and the server and the data format used in the communication.
1) The process of communication
Step1, the browser establishes a connection to the Web server
Step2, the browser packages the request data (generates a request packet) and sends it to the Web server.
The Step3,web Server packs processing results (generates response packets) and sends them to the browser.
The Step4,web server closes the connection.
If the browser wants to resend the request, a new connection will need to be re-established.
Features: One request, one connection .
Advantage: The Web server can use a limited number of connections for as many client (browser) services as possible.
2) Data format

A, Request packet

Such as:

[Plain]View PlainCopy
    1. Post/web02/hello http/1.1
    2. host:localhost:8080
    3. user-agent:mozilla/5.0 (Windows NT 5.1; rv:15.0) gecko/20100101 firefox/15.0.1
    4. accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
    5. accept-language:zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3
    6. Accept-encoding:gzip, deflate
    7. Connection:keep-alive
    8. Referer:http://localhost:8888/web02/hello_form.html
    9. content-type:application/x-www-form-urlencoded
    10. Content-length:16

Request Line: Request resource Path protocol type and version
Message header:
The message header is a number of key-value pairs, typically defined by the web, and can be sent between the browser and the server, indicating a specific meaning, for example, the browser can send a "user-agent" message header, tell the Web server browser type and version.
Entity content:
The request parameter is added to the request resource path only if the request method is post with a value (request parameter) and if the request is get.

B, Response packet

Such as:

[Plain]View PlainCopy
    1. http/1.1 OK
    2. server:apache-coyote/1.1
    3. Content-type:text/html;charset=utf-8
    4. Content-length:30
    5. Date:thu, 07:16:28 GMT

Status line: protocol type and version status Code status description
B1, what is a status code
is a three-digit number, defined by the consortium, that represents the state of the server processing requests.
B2, common status codes
200: Server processing request is normal.
404: The server cannot find the corresponding resource according to the request path.
500: System error, the server side of the program in the process of error.
405: The server cannot find an executable method to process the request.
Message header:
The server can also send some message headers to the browser, such as "Content-type", telling the browser, the type of data returned by the server and the encoding format (character set).
Entity content:
As a result of the process, the browser takes the data from the entity content and generates the corresponding page.

Ii. method of Request

1) in which case, the browser sends a GET request
A, enter an address directly in the browser address bar
b, click on the link
C, the form uses the default submission method
2) features of GET requests
A, the request parameters will be placed after the request resource path, only a small amount of data can be submitted (because the request line can hold up to about 2k of data).
b, the request parameters are displayed in the browser address bar, which is not secure (the router logs the request address).
3) in which case, the browser sends a POST request
Set the form's Method property value to post
4) Characteristics of POST request
A, the request parameters will be placed inside the entity content, can submit a large amount of data.
b, the request parameter is not displayed in the browser address bar, which is relatively secure (the browser is not encrypted).
Either get or post, the request parameters are not encrypted, so if it is sensitive data, encryption processing (HTTPS) is generally required.

The HTTP protocol supports six kinds of request methods, namely:
0,get
1,head
2,put
3,delete
4,post
5,options

6.trace

But in fact we use only get and post in most cases. These six methods are used if you want to design a Web application that is compliant with restful specifications. But even if you don't want to involve rest for the time being, understanding the nature of these six methods is still very useful. You will find that the web is also very concise and clear. The following six methods are described in turn.
0,get:get can be said to be the most common, it is essentially sending a request to get a resource on the server. Resources are returned to the client through a set of HTTP headers and rendering data (such as HTML text, or pictures or videos). In a GET request, the rendering data is never included.
The 1,head:head and get essence are the same, except that the head does not contain the rendering data, but only the HTTP header information. Some people may find this method useless, but that is not the case. Imagine a business scenario: to determine whether a resource exists, we usually use get, but the head here is more explicit.
2,put: This method is relatively rare. This is not supported by HTML forms. In essence, put and post are very similar, are sending data to the server, but there is an important difference between them, put usually specifies the location of the resources, and post is not, post data storage location by the server itself. For example, a url,/addblog for submitting a blog post. If put, the submitted URL will be "/addblog/abc123" like this, where abc123 is the address of the blog post. If you use post, the address will be communicated to the client by the server after submission. Most blogs are like this at the moment. Obviously, the put and post uses are not the same. The specific use depends on the current business scenario.
3,delete: Deletes a resource. This is mostly rare, but there are some places like Amazon's S3 cloud service that use this method to delete resources.
4,post: Submits data to the server. This method is widely used, and almost all of the current submissions are done by this.
5,options: This method is interesting, but rarely used. It is used to get the methods supported by the current URL. If the request succeeds, it contains a header named "Allow" in the HTTP header, which is the supported method, such as "GET, POST."
In fact, there is a trace method, but this basic will not be used, here is not introduced.

HTTP request/Response Boggs

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.